The end is generally converted into BufferedReader to read the string
InputStream instream = Request.getinputstream ();
BufferedReader br = new BufferedReader (new InputStreamReader (instream));
. InputStream, OutputStream
Abstract class for processing byte streams
InputStream is a superclass of all classes of byte input streams, generally we use its subclasses, such as FileInputStream.
OutputStream is a superclass of all classes of byte output streams, generally we use its subclasses, such as FileOutputStream.
2.InputStreamReader OutputStreamWriter
Abstract class for handling character streams
InputStreamReader is a bridge of byte flow to a character stream, which converts a stream of bytes into a character stream.
OutputStreamWriter is a bridge of character flow to a byte stream, which converts a stream of characters into a byte stream.
3.BufferedReader BufferedWriter
The BufferedReader is extended by the reader class, providing a common buffered way to read text, ReadLine reading a line of text,
Reads text from the character input stream, buffering individual characters, providing efficient reading of characters, arrays, and rows.
The BufferedWriter is extended by the writer class, providing a common buffer for text writing, newline using the platform's own line delimiter,
Writes text to the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings.
InputStream can read a byte from the source,
So it's the lowest level,
Cases:
[Java]View PlainCopy
- Import <a href="HTTP://LIB.CSDN.NET/BASE/17" class=' Replace_word ' title="Java ee Knowledge Base" target=' _blank ' style=' color: #df3434; font-weight:bold; ' >Java</a>.io.*;
- Public class Main {
- public static void Main (string[] args) {
- try {
- FileInputStream fis=New FileInputStream ("D://desktop//test.txt");
- int i;
- try {
- While ((I=fis.read ())! =-1) {
- System.out.println (i);
- }
- / * The Test.txt file has only one word "Chan" and is encoded as UTF8
- * Input Output
- * 233
- 153
- 179
- */
- } catch (IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- } catch (FileNotFoundException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
InputStreamReader
InputStreamReader sealed the InputStream inside,
It reads one character at a time in a higher-level way,
The following example assumes that there is a document encoded as UTF8,
It has only one Chinese character, "Chan".
[Java]View PlainCopy
- Import java.io.*;
- Public class Main {
- public static void Main (string[] args) throws FileNotFoundException, unsupportedencodingexception {
- FileInputStream fis=New FileInputStream ("D://desktop//test.txt");
- try {
- InputStreamReader isr=New InputStreamReader (FIS,"UTF8");
- int i;
- While ((I=isr.read ())! =-1) {
- System.out.println ((char) i); //Output chan
- }
- } catch (Exception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
BufferedReader
BufferedReader is more advanced than InputStreamReader,
It sealed the StreamReader class,
Read a row of characters at a time
[Java]View PlainCopy
- Import java.io.*;
- Public class Main {
- public static void Main (string[] args) throws FileNotFoundException, unsupportedencodingexception {
- FileInputStream fis=New FileInputStream ("D://desktop//test.txt");
- try {
- InputStreamReader isr=New InputStreamReader (FIS,"UTF8");
- BufferedReader br=New BufferedReader (ISR);
- String Line;
- While ((Line=br.readline ()) = null) {
- System.out.println (line);
- }
- } catch (Exception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
InputStream conversion InputStreamReader re-conversion BufferedReader