Explanation of principle: http://ifeve.com/java-io-char-buffered-filter/
Bufferedinputstream byte stream Input buffer:
1 voidBufferedinputstream () {2 //Bufferedinputstream with buffers, multithreading visible3 //Reading Chinese will be garbled4 //no ReadLine ()5File File =NewFile ("E:\\abcdata.txt");6InputStream InputStream =NULL;7Bufferedinputstream bis =NULL;8 byte[] B =New byte[1024];9 intLen;TenStringBuilder SB =NewStringBuilder (); One Try { AInputStream =Newfileinputstream (file); -bis =NewBufferedinputstream (inputstream); - while(len = Bis.read (b))! =-1) { theString string =NewString (b, 0, Len, "UTF-8")); - sb.append (string); - } - System.out.println (SB); +}Catch(FileNotFoundException e) { - //TODO auto-generated Catch block + e.printstacktrace (); A}Catch(IOException e) { at //TODO auto-generated Catch block - e.printstacktrace (); -}finally { - if(bis! =NULL) { - Try { - bis.close (); in}Catch(IOException e) { - //TODO auto-generated Catch block to e.printstacktrace (); + } - } the } * $}
Bufferedoutputstream byte throttle Output buffer:
1 voidBufferedoutputstream () {2File File =NewFile ("E:\\abcdata.txt");3OutputStream OutputStream =NULL;4Bufferedoutputstream BOS =NULL;5 byte[] B =New byte[1024];6String s = "Test output!! ";7 Try {8OutputStream =NewFileOutputStream (file,true);9BOS =NewBufferedoutputstream (outputstream);Tenb =s.getbytes (); One Bos.write (b); A //Refresh Buffering - Bos.flush (); -}Catch(FileNotFoundException e) { the //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { - //TODO auto-generated Catch block + e.printstacktrace (); -}finally { + if(Bos! =NULL) { A Try { at bos.close (); -}Catch(IOException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } - } in } -}
BufferedReader character stream Input buffer:
1 voidBufferedReader () {2File File =NewFile ("E:\\test.txt");3BufferedReader br =NULL;4Reader reader =NULL;5 string string;6StringBuilder SB =NewStringBuilder ();7 Try {8Reader =Newfilereader (file);9BR =NewBufferedReader (reader);Ten while(String = Br.readline ())! =NULL) { One sb.append (string); A } - System.out.println (SB); -}Catch(FileNotFoundException e) { the //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(IOException e) { - //TODO auto-generated Catch block + e.printstacktrace (); -}finally { + if(BR! =NULL) { A Try { at br.close (); -}Catch(IOException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } - } in } - to}
BufferedWriter character stream output buffering:
1 voidBufferedWriter () {2File File =NewFile ("E:\\test.txt");3Writer writer =NULL;4BufferedWriter BW =NULL;5String string = "Test Tests!! ";6 Try {7writer =NewFileWriter (file,true);8BW =NewBufferedWriter (writer);9 bw.write (string);Ten Bw.flush (); One}Catch(IOException e) { A //TODO auto-generated Catch block - e.printstacktrace (); -}finally { the if(BW! =NULL) { - Try { - bw.close (); -}Catch(IOException e) { + //TODO auto-generated Catch block - e.printstacktrace (); + } A } at } - -}
IO: Buffering