Inputstream provides byte stream reading instead of text reading. Reader reads char arrays or strings and inputstream reads byte arrays.
The reader class and its subclass provide the reading char of the bytes stream, and the inputstream and its subclass provide the reading byte of the byte stream. Therefore, the filereader class reads the file in the form of the bytes stream, fileinputstream reads files in byte streams. inputstreamreader can convert read data such as stream into bytes streams, which serves as a bridge between reader and stream.
1. String --> inputstream
Public inputsteam getinputstreamfromstring (string Str) {</P> <p> inputsteam in = new bytearrayinputstream (Str. getbytes (); <br/>}< br/>
2. inputstream --> string
Public String getstrfrominputsteam (inputsteam in) {<br/> bufferedreader BF = new bufferedreader (New inputstreamreader (in, "UTF-8 ")); <br/> // it is best to transcode the byte stream when it is switched to the bytes stream <br/> stringbuffer buffer = new stringbuffer (); <br/> string line = ""; <br/> while (line = BF. readline ())! = NULL) {<br/> buffer. append (line); <br/>}</P> <p> return buffer. tostring (); <br/>}
Here, of course, you can also return the string without stringbuffer, but I personally think it is better to use stringbuffer. The while loop can be used.
Ioutils. readlines (buffer). tostring ();