5 ways to copy a text file in Java 21-8

Source: Internet
Author: User

Requirements: Copying text files
Analysis:
Because the text file we open with Notepad can be read, so use a character stream.
  There are 5 ways to copy a character stream:


First write the Main method

1      Public Static voidMain (string[] args)throwsIOException {2         //Data Source3String startfile = "c:\\users\\administrator\\desktop\\ new Folder \\start.txt";4         //Destination5String endfile = "c:\\users\\administrator\\desktop\\ new Folder \\end.txt";6         7         //method1 (startfile,endfile);8         //method2 (startfile,endfile);9         //method3 (startfile,endfile);Ten         //method4 (startfile,endfile); One Method5 (startfile,endfile); A  -}

The first way: the basic character stream reads one character at a time

1     Private Static voidMethod1 (String startfile,string endfile)throwsIOException {2         //basic character stream reads and writes one character at a time3         //Encapsulating Data Sources4FileReader FR =NewFileReader (startfile);5         //Package Destination6FileWriter FW =NewFileWriter (endfile);7         8         //Read and write one character at a time9         intCH = 0;Ten          while(ch = fr.read ())! =-1){ One fw.write (CH); A         } -         //Freeing Resources - fr.close (); the fw.close (); -}

The second way: the basic character stream reads one character array at a time

1     Private Static voidMETHOD2 (String startfile, String endfile)throwsIOException {2         //The basic character stream reads and writes one character array at a time3FileReader FR =NewFileReader (startfile);4FileWriter FW =NewFileWriter (endfile);5         6         Char[] ch =New Char[1024];7         intLen = 0;8          while(len = fr.read (ch))! =-1){9Fw.write (Ch,0,len);//we need to be awareTen         } One fr.close (); A fw.close (); -}

Third Way: Buffer character Stream reads one character at a time

1     Private Static voidMethod3 (String startfile, String endfile)throwsioexception{2         //character Buffer Stream reads one character at a time3BufferedReader br =NewBufferedReader (NewFileReader (Startfile));4BufferedWriter BW =NewBufferedWriter (NewFileWriter (Endfile));5         intCH = 0;6          while(ch = br.read ())! =-1){7 bw.write (CH);8         }9 br.close ();Ten bw.close (); One          A}

Fourth Way: Buffer character Stream reads one character array at a time

1     Private Static voidMETHOD4 (String startfile, String endfile)throwsIOException {2         //character Buffer stream reads one character array at a time3BufferedWriter BW =NewBufferedWriter (NewFileWriter (Endfile));4BufferedReader br =NewBufferedReader (NewFileReader (Startfile));5         Char[] ch =New Char[1024];6         intLen = 0;7          while(len = br.read (ch))! =-1){8Bw.write (ch,0, Len);9         }Ten br.close (); One bw.close (); A          -}

Fifth way: Buffer character stream read one string at a time (master)

    Private Static voidMETHOD5 (String startfile, String endfile)throwsIOException {//character Buffer stream reads one string at a timeBufferedReader br =NewBufferedReader (NewFileReader (startfile)); BufferedWriter BW=NewBufferedWriter (NewFileWriter (endfile)); String Line=NULL;  while(line = Br.readline ())! =NULL) {bw.write (line);                Bw.newline ();            } br.close ();                Bw.close (); }

5 ways to copy a text file in Java 21-8

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.