Java files and io--buffered streams

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.