/**
*不带缓冲的文件拷贝!效率最低
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile01
{
public static void main(String[] args) throws Exception
{
if(args.length != 2)
{
System.out.println("用法:java CopyFile01 源文件 目标文件");
System.exit(0);
}
FileInputStream in = new FileInputStream(args[0]);
FileOutputStream out = new FileOutputStream(args[1]);
int x = 0;
long start = System.currentTimeMillis();
while((x = in.read()) != -1)
{
out.write(x);
}
long end = System.currentTimeMillis();
System.out.println(args[0] + "拷贝完成!" + args[1] + "总共花费时间" + (end - start) + "毫秒!");
in.close();
out.close();
}
}
/**
*带缓冲的文件拷贝!效率最高
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile02
{
public static void main(String[] args) throws Exception
{
if(args.length != 2)
{
System.out.println("用法:java CopyFile01 源文件 目标文件");
System.exit(0);
}
FileInputStream in = new FileInputStream(args[0]);
FileOutputStream out = new FileOutputStream(args[1]);
int len = 0;
byte[] buf = new byte[1024]; //自定义一个1024字节的缓冲区
long start = System.currentTimeMillis();
while((len = in.read(buf)) != -1)
{
out.write(buf, 0, len);
}
long end = System.currentTimeMillis();
System.out.println(args[0] + "拷贝完成!" + args[1] + "总共花费时间" + (end - start) + "毫秒!");
in.close();
out.close();
}
}
/**
*自定义的带缓冲的文件拷贝!效率最高
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
public class CopyFile03
{
public static void main(String[] args) throws Exception
{
if(args.length != 2)
{
System.out.println("用法:java CopyFile01 源文件 目标文件");
System.exit(0);
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0]));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(args[1]));
int x = 0;
long start = System.currentTimeMillis();
while((x = bis.read()) != -1)
{
bos.write(x);
}
long end = System.currentTimeMillis();
System.out.println(args[0] + "拷贝完成!" + args[1] + "总共花费时间" + (end - start) + "毫秒!");
bis.close();
bos.close();
}
}