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)