In the Java IO Stream read and write files, always by its various streams can be very confusing, there are more than 40 classes, clear, after a period of confusion, decided to tidy up! In case you forget again.
Java input/output stream architecture
1. Byte stream and character stream
byte stream: Read by bytes. character stream: read by characters.
The character stream reads conveniently, the character stream function is powerful, when cannot use the character stream, may use the byte stream.
BYTE stream base class: InputStream, OutputStream
InputStream Method:
OutputStream Method:
Character stream base class: Reader, Writer
Reader method:
Writer Method:
2. Input/output stream system
3. Conversion flow
InputStreamReader: Conversion of character input stream, byte input stream
OutputStreamWriter: Conversion of character output stream, byte output stream
Example: conversion of character input stream, byte input stream
Byte stream is inconvenient to use, and ordinary reader reading is not very convenient, converted to Bufferreader, using Bufferreader's ReadLine (), read one line at a time.
New BufferedReader (new InputStreamReader (InputStream)); "" ; "" ; while NULL { = result+ line ; } System. out. println (result);
InputStream to String:
Private StaticString Changeinputestream (inputstream inputstream,string encode) {//usually called a memory stream, written in memory.Bytearrayoutputstream OutputStream =NewBytearrayoutputstream (); byte[] data =New byte[1024x768]; intLen =0; String result=""; if(InputStream! =NULL){ Try { while(len = inputstream.read (data))!=-1) {outputstream.write (data,0, Len); } //result is in the Dopost function set on the server side.result =NewString (Outputstream.tobytearray (), encode); Outputstream.flush (); Outputstream.close (); Inputstream.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnresult; }
Inputstream->stringbuffer:
StringBuffer buffer =NewStringBuffer (); InputStreamReader Reader=NewInputStreamReader (Request.getinputstream (),"Utf-8"); Char[] Requestchar =New Char[5]; if(Reader! =NULL) {System. out. println ("default encoding:"+reader.getencoding ());//default encoding: UTF8 } //buffer Reader.readline () while(temp = Reader.read (Requestchar))!=-1) {buffer.append (Requestchar,0, temp); System. out. println (Buffer.tostring ()); The String result= Buffer.tostring ();
4. File read and write:
classkaoshi{ Public Static voidMain (string[] args) {Scannerinch=NewScanner (System.inch); intScore=0;//score;StringBuffer answer=NewStringBuffer ();//Store user answers, dynamic strings;String result="ACDD";//The right answer;Try{FileReader file=NewFileReader ("Test.txt");//open the file;BufferedReader intw=NewBufferedReader (file);//buffer stream, read by line;String s=NULL; while((S=intw.readline ())! =NULL){//the judgment is not empty; if(!s.startswith ("*") ) {System. out. println (s); } Else{System. out. println ("Please enter the correct answer:"); String Str=inch. Next (); CharC= Str.charat (0);//collect answers;Answer.append (c); } } }Catch(IOException e) {e.printstacktrace (); } for(intI=0; I<result.length (); i++){ if(Result.charat (i) ==answer.charat (i) | |Result.charat (i)==answer.charat (i)- +) {score+= -; }} System. out. println (score);} }
Java input/output stream architecture