Enter the conversion stream for the byte stream: InputStreamReader is a bridge of byte flow to character stream
conversion stream for output byte stream: OutputStreamWriter can change the output byte stream into the output character streams.
The role of the transform stream:
1. If the current acquisition is a stream of bytes that needs to be converted to the flow of characters, then the conversion stream can be used. BYTE stream----> character stream
2. Use the conversion stream to specify the encoding table to read and write files.
//reads the file data using the input byte stream's conversion stream to specify the code table Public Static voidReadTest2 ()throwsioexception{File File=NewFile ("F:\\a.txt"); FileInputStream FileInputStream=Newfileinputstream (file); //create a stream of streams for the byte stream and specify the code table to readInputStreamReader InputStreamReader =NewInputStreamReader (FileInputStream, "Utf-8"); Char[] buf =New Char[1024]; intLength = 0; while(length = Inputstreamreader.read (BUF))!=-1) {System.out.println (NewString (buf,0, length)); } } //use the output byte stream to specify the code table to write data Public Static voidWriteTest2 ()throwsioexception{File File=NewFile ("F:\\a.txt"); //set up the output channel of the dataFileOutputStream FileOutputStream =Newfileoutputstream (file); //Converts the output byte stream into a character flow and specifies the encoding table. OutputStreamWriter OutputStreamWriter =NewOutputStreamWriter (FileOutputStream, "Utf-8"); Outputstreamwriter.write ("New China is good."); //Close ResourceOutputstreamwriter.close (); } Public Static voidWritetest ()throwsioexception{File File=NewFile ("F:\\a.txt"); FileOutputStream FileOutputStream=Newfileoutputstream (file); //convert the output byte stream to the output character streams. OutputStreamWriter OutputStreamWriter =NewOutputStreamWriter (FileOutputStream); Outputstreamwriter.write ("Good for everyone."); Outputstreamwriter.close (); } Public Static voidReadtest ()throwsioexception{InputStream in= system.in;//gets the standard input stream. System.out.println ("Read characters:" + (Char) In.read ());//Read () can read only one byte at a time. //the byte stream needs to be converted into character streams. InputStreamReader InputStreamReader =NewInputStreamReader (in); //buffering classes that use character streamsBufferedReader BufferedReader =NewBufferedReader (InputStreamReader); String Line=NULL; while(line = Bufferedreader.readline ())! =NULL) {System.out.println ("Content:" +Line ); } }
InputStreamReader and OutputStreamWriter