(a) One byte
Import java.io.*;
/**
* Implement file copy
* @author Administrator
*
*/
public class CopyFile {
public static void Copybytesfile (String sfilename,string dfilename) throws ioexception{
Creating 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 file
Use a loop to read the contents of a file and write it out to an external disk file
Reads 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 bytes
int Read=fis.read ();
while (Read!=-1) {
Fos.write (read);//writes out byte data in the current byte array to the associated target file
Read=fis.read ();
}
Fis.close ();//clear cache, close stream object
Fos.close ();
}
/**
* @param args
*/
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) + "MS");
}
}
(b) Multiple bytes
Import java.io.*;
/**
* Implement file copy
* @author Administrator
*
*/
public class CopyFile {
public static void Copybytesfile (String sfilename,string dfilename) throws ioexception{
Creating 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 file
Use a loop to read the contents of a file and write it out to an external disk file
Byte[] b= new byte[1024];
Read multiple bytes from the associated source file, save to byte array B, if the end of the file is reached, the return value is-1, otherwise the return value is the actual unique number of bytes
int Read=fis.read (b);
while (Read!=-1) {
Fos.write (b);//writes out byte data in the current byte array to the associated target file
Read=fis.read (b);
}
Fis.close ();//clear cache, close stream object
Fos.close ();
}
/**
* @param args
*/
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) + "MS");
}
}
Java fourth time job