The Java Buffer stream itself does not have IO functionality, but adds buffering to 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 streamsthe byte buffer stream is:bufferedinputstream-byte input buffer streambufferedoutputstream-byte output buffered streamthe character Buffer stream is:bufferedreader-character input buffer streambufferedwriter-character output buffer streamThe following mainly describes the use of these four kinds of buffer streams. A. Byte buffer stream1.bufferedoutputstream-byte output buffered streamThe 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: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 Code2.bufferedinputstream-byte input buffer streamBufferedinputstream adds buffering capabilities to other input streams, creating an internal buffer array to buffer the data and improve performance when creating Bufferedinputstream. 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 CodeOperation Result: <ignore_js_op>
two. Character Buffer stream1.bufferedwriter-character output buffer streamwrites 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. 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 CodeOperation Result: <ignore_js_op> 2.bufferedreader-character input buffer streamreads 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 CodeOperation Result: <ignore_js_op> |
"Buffer stream of Io stream for Java"