file copy import java.io.*;public class fileCopy {public static void main(String[] args) throws Exception{File file =new File("F:/workspace/Photoshop CC_CHS(www.greenxf.com).rar");File new_file=new File("E:/ps.rar");long start=System.curren
import java.io.*;
public class fileCopy
{
public static void main(String[] args) throws Exception
{
File file =new File("F:/workspace/Photoshop CC_CHS(www.greenxf.com).rar");
File new_file=new File("E:/ps.rar");
long start=System.currentTimeMillis();
copy(file,new_file);
long end=System.currentTimeMillis();
System.out.println(end-start);//8000
System.out.println("--------------");
long start1=System.currentTimeMillis();
copy1(file,new_file);
long end1=System.currentTimeMillis();
System.out.println(end1-start1);//3000
}
//1、字节流读取字节数组:
public static void copy(File file,File new_file){
try{
FileInputStream fis =new FileInputStream(file);
FileOutputStream fos =new FileOutputStream(new_file);
byte[] bf= new byte[1024];
int by=0;
while((by=fis.read(bf))!=-1){
//read(by)从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中
//read(by)返回读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
fos.write(bf,0,by);//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
}
fos.close();
fis.close();
}catch(Exception e){
}
}
//2、缓冲区对象读取字节数组:读取速度最快
public static void copy1(File file,File new_file){
try{
FileInputStream fis =new FileInputStream(file);
BufferedInputStream bi=new BufferedInputStream(fis);
FileOutputStream fos =new FileOutputStream(new_file);
BufferedOutputStream bo=new BufferedOutputStream(fos);
byte[] bf= new byte[1024];
int by=0;
while((by=bi.read(bf))!=-1){
//read(by)从此输入流中将最多 by.length 个字节的数据读入一个 byte 数组中
//read(by)返回读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
bo.write(bf,0,by);//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
}
fos.close();
fis.close();
}catch(Exception e){
}
}
//3、单个字节读取
//FileInputStream fis=new FileInputStream(“……”);
//Fis.read();
//这种方式每次只获取一个字节的数据,而要复制的数据变大时,就要增加大量的循环次数,才可以把所有数据复制完,
//——速度是相当的慢,平时开发今本不用这种复制方式。
//4、利用缓冲区对象读取单个字节
}
