Java The buffer stream itself has no IO function, but adds buffering to the other streams to improve efficiency, such as loading a wrapper over another stream . When the file or other target frequently read or write or operate inefficient, poor performance. This allows for more efficient reading and writing of information using buffered streams. Because the buffered stream caches the data first and then writes or reads it together. So, the buffer stream is still very important, in the IO operation remember to add buffer stream to improve performance.
Buffer stream divided into byte and character buffer streams The byte buffer stream is: bufferedinputstream-byte input buffer stream bufferedoutputstream-byte output buffered stream The character Buffer stream is: bufferedreader-character input buffer stream bufferedwriter-character output Buffer stream The following mainly describes the use of these four kinds of buffer streams.
A. Byte buffer stream 1.bufferedoutputstream-byte output buffered stream The Bufferedoutputstream class implements buffered output, and by setting this output stream, the application can write individual bytes to the underlying output stream without having to call the underlying system for every byte write. The constructor for this class: 650) this.width=650; "id=" aimg_299 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132740e0hp5nif7h47x70h.png "class=" Zoom "width=" 577 "alt=" 132740e0hp5nif7h47x70h.png "/> Example code:
public static void Main (string[] args) {
try {
Creating a byte output stream instance
OutputStream out=new FileOutputStream ("L:\\test.txt");
Build byte buffer stream based on byte output stream
Bufferedoutputstream buf=new Bufferedoutputstream (out);
String data= "Good study, day up";
Buf.write (Data.getbytes ());//write Buffer
Buf.flush ();//flush buffer, i.e. write content to
Close the stream
Buf.close ();//The buffer is also refreshed once the buffer stream is closed
Out.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code 2.bufferedinputstream-byte input buffer stream Bufferedinputstream adds buffering capabilities to other input streams, creating an internal buffer array to buffer the data and improve performance when creating Bufferedinputstream. 650) this.width=650; "id=" aimg_300 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132807szrd2hb3qzqdv3wd.png "class=" Zoom "width=" 504 "alt=" 132807szrd2hb3qzqdv3wd.png "/> Example code:
public static void Main (string[] args) {
try {
Create a byte input stream instance
InputStream in=new FileInputStream ("L:\\test.txt");
Building byte buffer streams based on byte input stream
Bufferedinputstream buf=new Bufferedinputstream (in);
Byte[]bytes=new byte[1024];
Data read
int len=-1;
StringBuffer sb=new StringBuffer ();
while ((Len=buf.read (bytes))!=-1)
{
Sb.append (New String (Bytes,0,len));
}
System.out.println ("The content is:" +SB);
Close the stream
Buf.close ();
In.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code Operation Result: 650) this.width=650; "id=" aimg_301 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132834h6frj37j87jrzff8.png "class=" Zoom "width=" 301 "alt=" 132834h6frj37j87jrzff8.png "/>
Two. Character Buffer stream 1.bufferedwriter-character output Buffer stream Writes text to the character output stream, buffering individual characters, providing efficient writes. You can specify the size of the buffer, which, in general, is sufficient for the default buffer size. 650) this.width=650; "id=" aimg_302 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132856uylv2mv2s2jw2m5m.png "class=" Zoom "width=" 538 "alt=" 132856uylv2mv2s2jw2m5m.png "/> Example code:
public static void Main (string[] args) {
try {
Writer w=new FileWriter ("L:\\test.txt");
To create a character buffer stream from a character output stream
BufferedWriter buf=new BufferedWriter (w);
Write Data
Buf.write ("Kong Mulberry becomes Satin");
Refresh Stream
Buf.flush ();
Close the stream
Buf.close ();
W.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code Operation Result: 650) this.width=650; "id=" aimg_303 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132925n4jq22bbqc12bzjk.png "class=" Zoom "width=" 499 "alt=" 132925n4jq22bbqc12bzjk.png "/>
2.bufferedreader-character input buffer stream Reads information from the character input stream, buffering individual characters for efficient reading. You can specify the size of the buffer, which, in general, is sufficient for the default buffer size. The default size is 8192.
Example code:
public static void Main (string[] args) {
try {
Reader r=new FileReader ("L:\\test.txt");
To create a character buffer stream from a character input stream
BufferedReader buf=new BufferedReader (R);
Char [] data=new char[512];
Data read
int len=-1;
StringBuilder sb=new StringBuilder ();
while ((Len=buf.read (data))!=-1)
{
Sb.append (New String (Data,0,len));
}
System.out.println ("Content is:" +SB);
Close the stream
Buf.close ();
R.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Copy Code Operation Result: 650) this.width=650; "id=" aimg_304 "src=" http://techfoxbbs.com/data/attachment/forum/201505/22/ 132957vzfact8cchsct8xv.png "class=" Zoom "width=" 317 "alt=" 132957vzfact8cchsct8xv.png "/> |