Directly on the source:
1 PackageCopyFile;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 /**9 * Preparatory work: Prepare a MP3 music, named 1.mp3. Copy the music file to the D drive. The path to the file is D:\1.mp3Ten * Mission Purpose: To achieve the replication of files. Copy the 1.mp3 to 2.mp3. One */ A Public classFileCopy { - Public Static voidMain (string[] args) { -FileInputStream FIS =NULL; theFileOutputStream fos =NULL; -File sourcefile =NewFile ("D:/1.mp3");//source File -File targetfile =NewFile ("D:/2.mp3");//target file - byte[] buf =New byte[1024];//create a buffer of 1K size + if(!sourcefile.exists ()) {//determine if the source file exists, exit the program if it does not exist -System.out.println ("Source file does not exist"); + return; A } at if(Targetfile.exists ()) {//when the target file is determined to exist, it is deleted. - Targetfile.delete (); - } - Try { -Targetfile.createnewfile ();//Create an empty destination file -}Catch(IOException e) { in e.printstacktrace (); - } to Try { +FIS =NewFileInputStream (sourcefile);//gets the input stream of the source file -FOS =NewFileOutputStream (targetfile);//gets the output stream of the destination file the while(Fis.read (BUF)!=-1) {//while loop, the file starts copying * Fos.write (BUF); $ }Panax NotoginsengFis.close ();//close the input stream -Fos.close ();//turn off the output stream the}Catch(Exception e) { + e.printstacktrace (); A } the } +}
Java implementation File replication