Java fourth time job

Source: Internet
Author: User

(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

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.