Java 21-6 character Buffer stream special method and the method to efficiently copy files

Source: Internet
Author: User
Tags readline

Special methods for character buffer streams:
A, Bufferedwriter:
    public void NewLine (): based on system to determine line break

1     Private Static voidWrite ()throwsIOException {2         //creating a character output buffer stream3BufferedWriter BW =NewBufferedWriter (NewFileWriter ("bw.txt"));4          for(intx = 0;x < 10; X + +){5Bw.write ("java" +x);6             //automatically wrap every time.7 Bw.newline ();8         }9         //Freeing ResourcesTen bw.close (); one}

B, Bufferedreader:
    Public String ReadLine (): reads one row of data at a time
  note: A string that contains the contents of the row, does not contain any line terminators, and returns null if the end of the stream has been reached

1     Private Static voidRead ()throwsIOException {2         //creating a character input buffer stream3BufferedReader br =NewBufferedReader (NewFileReader ("bw.txt"));4         //read one row at a time5         //with a loop, the terminating condition is to return null6String str =NULL;//reading is a string7          while(str = Br.readline ())! =NULL){8System.out.println (str);//Print out the contents of the Bw.txt.9         }Ten         //Freeing Resources one br.close (); a}

Use character buffer streams to copy text files: be familiar with

     public Static voidMain (string[] Args)throwsIOException {//Encapsulating Data SourcesBufferedReader br =NewBufferedReader (NewFileReader ("a.txt")); //Package DestinationBufferedWriter BW =NewBufferedWriter (NewFileWriter ("copy.txt")); //copy in a newline-read mannerString len =NULL;  while(len = Br.readline ())! =NULL){            //output to the copy file if there is contentBw.write (len); //Note that this is going to be a wrap, or the content will be Connected.Bw.newline (); }        //Freeing ResourcesBr.close ();    Bw.close (); }

Java 21-6 character Buffer stream special method and the method to efficiently copy files

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.