For example, to read bytes from the file used InputStream
, BufferedReader
, etc.
I met this: bytearrayInputStream
.
The description says:
represents the input stream using data source
Byte array
What files to apply it?
I saw an example in which the usual text file is read by this method.
How to determine what should be used?
Here is an example:
string getstringfromrawfile (Activity Activity) {
Resources R = Activity.getResources ();
INPUTSTREAM IS = R.OPENRAWRESOURCE (R.RAW.TEST);
String MyText = ConvertStreamToString (IS);
is.close ();
RETURN MYTEXT;
}
String ConvertStreamTostRing (InputStream IS) {
Bytearrayoutputstream baos = new bytearrayoutputstream ();
int i = is.read ();
While (i! = -1)
{
BAOS.WRITE (I);
i = baos.read ();
}
Return baos.tostring ();
}
Answer 1, Authority 100%
Not each thread is designed to read from the file. Strictly speaking, only the file reads FileInputStream
– the remaining streams read from somewhere else.
BytearrayInputStream
you need when you need to read data from the byte array. It usually makes sense to use it not by itself, but in combination with another code that can read from any inputstream
. For example, if you have an array byte, in which the XML is recorded, and you need to paint it, building the DOM – that BytearrayInputStream
will be exactly what you need.
Answer 2
BytearrayInputStream as well as BYTEARAYOUTPUTStReam – Simulation of the implementation of InputStream and OutputStream interfaces above the bytes array.
The easiest example, how to display E.PrintStackTrace in a line for further processing (well, suppose that so necessary)?
bytearrayoutputstream out = new bytearrayoutputstream ();
E.PrintStackTrace (New Printwriter (OUT));
String Stack = Out.Tostring ();
and other ways, sometimes no.