(vi) Two ways to copy the contents of one file to another (the second way is more efficient, the first one character reads and writes, the second is an array of an array of reads and writes)

Source: Internet
Author: User
Tags stub

One : Copy one file to another file, one character at a time
Importjava.io.FileNotFoundException;ImportJava.io.FileReader;ImportJava.io.FileWriter; Public classCopyFile {/*** Requirements: Copy one file to another file *@paramargs *@throwsException*/ Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stub//read an existing fileFileReader FR =NewFileReader ("D:\\log.txt"); //Create a purpose for storing read dataFileWriter FW =NewFileWriter ("D:\\fuyanan.txt"); //read data frequently, one character at a time intCH =Fr.read (); while(ch = fr.read ())! =-1) {fw.write (CH); } //Turn off Data flowFw.close (); Fr.close (); }}

Two, copy one file to another file, each time read BUF length

ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException; Public classCopyFile2 {Private Static Final intBuffer_size = 1024; /**     * @paramargs *@throwsIOException*/     Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method StubFileReader FR =NULL; FileWriter FW=NULL; Try{FR=NewFileReader ("D:\\log.txt"); FW=NewFileWriter ("D:\\fuluolin.txt"); //creates a temporary container for caching read-to characters            Char[] buf =New Char[Buffer_size]; //defines the number of characters that a variable record reads, (in fact, the character is loaded into the array)            intLen = 0;  while(len = Fr.read (BUF))! =-1) {fw.write (buf,0, Len); }        } Catch(Exception e) {Throw NewRuntimeException ("Read and Write Failed"); } finally {            if(FW! =NULL) Fw.close (); if(FR! =NULL) Fr.close (); }    }}

(vi) Two ways to copy the contents of one file to another (the second way is more efficient, the first one character reads and writes, the second is an array of an array of reads and writes)

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.