Import java.io.*;PublicClassCopyfile {PublicStaticvoidCopybytesfile(String sfilename,string Dfilename)Throws ioexception{Create a file input stream object and a file output stream object FileInputStream fis=New FileInputStream (sFileName);File input stream object associated with source file FileOutputStream fos=New FileOutputStream (Dfilename);File output stream object associated with target fileUse a loop to read the contents of a file and write it out to an external disk fileReads a byte from the associated source file, if the end of the file is reached, the return value is-1, otherwise the return value is the actual unique number of bytesint Read=fis.read ();while (read!=-1) {fos.write (read);writes out the byte data in the current byte array to the associated target file Read=fis.read ();} Fis.close (); //Empty cache, Close stream object Fos.close ();} public static void main(string[] args) {//TODO auto-generated method stubLong t1,t2;t1= System.currenttimemillis (); try {copyfile.copybytesfile ("A.mp3", "Temp.mp3");} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();} t2=system.currenttimemillis () ; System.out.println ("spents:" + (T2-T1) +"MS");}}
Multi-character output
Import Java.io.FileInputStream;Import java.io.FileNotFoundException;Import Java.io.FileOutputStream;Import java.io.IOException;PublicClassCopyfile {PublicStaticvoidCopybytesfile(String sfilename,string Dfilename)Throws ioexception{Creating a file input stream object and a file output stream objectFile input stream object associated with source file FileInputStream fis=New FileInputStream (sFileName);File output stream object associated with target file FileOutputStream fos=New FileOutputStream (Dfilename);Loop through the contents of the file and write it out to the external fileByte[] B=Newbyte[1024*10];Reads a byte from the associated source file, saves it to byte array B, and returns a value of 1 if it reaches the end of the file.int read =fis.read (b);while (read!=-1) {Fos.write (b); //writes out a byte to the associated target file Read=fis.read (b);} //empties the cache, closes the Stream object Fis.close (); Fos.close ();} public static void main (string[] args) { //TODO auto-generated Method stub long t1,t2; t1= System.currenttimemillis (); try {copyfile.copybytesfile ( "A.mp3", "Temp.mp3"); } catch (IOException e) {//TODO auto-generated Catch block E.printstacktrace (); } t2=system.currenttimemillis (); System.out.println ( "spents:" + (T2-T1));}}
Fourth time assignment