Frequent read and write operations on files or other targets with low efficiency and poor performance.
The benefit of using buffered streams is to be able to read and write information more efficiently, by buffering the data first and then writing or reading it together.
Bufferedinputstream: Adds some functionality to another input stream that, when created, creates an array of internal buffers to buffer the data
Bufferedoutputstream: By setting this output stream, the application can write individual bytes to the underlying output stream without having to call the underlying system for each byte write
BufferedReader: Reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows.
BufferedWriter: Writes text to the character output stream, buffering individual characters, providing efficient writes of individual characters, arrays, and strings.
Public classBufferedstreamdemo {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method Stubbytestreamout (); Bytestreamin (); Charstreamout (); Charstreamin (); } //Bufferedoutputstream Public Static voidbytestreamout () {Try{OutputStream out=NewFileOutputStream ("G:\\hhb.txt"); Bufferedoutputstream Bos=NewBufferedoutputstream (out); Bos.write ("Hhhhh". GetBytes ()); Bos.close (); Out.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //Bufferedinputstream Public Static voidbytestreamin () {Try{InputStream in=NewFileInputStream ("G:\\hhb.txt"); Bufferedinputstream bis=NewBufferedinputstream (in); byte[] bytes=New byte[1024]; intLen=-1; StringBuffer SB=NewStringBuffer (); while((Len=bis.read (bytes))!=-1) {sb.append (NewString (Bytes, 0, Len)); } bis.close (); In.close (); System.out.println (SB); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //BufferedWriter Public Static voidcharstreamout () {Try{Writer W=NewFileWriter ("G:\\hhh.txt"); BufferedWriter BW=NewBufferedWriter (W); Bw.write ("I love you!" "); Bw.close (); W.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } //BufferedReader Public Static voidcharstreamin () {Try{Reader R=NewFileReader ("G:\\hhh.txt"); BufferedReader BR=NewBufferedReader (R); Char[] c=New Char[1]; intLen =-1; StringBuffer SB=NewStringBuffer (); while((Len=br.read (c))!=-1) {sb.append (c,0, Len); } br.close (); R.close (); System.out.println (SB); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Java files and io--buffered streams