Java(字节流实现文件拷贝)
文件拷贝
/*
文件拷贝:一读一写
实现步骤:
1.创建一个字节输入流对象,构造方法中绑定要读取的数据源
2.创建一个字节输出流对象,构造方法中绑定要写入的目的地
3.使用字节输入流对象中的方法read读取文件
4.使用字节输出流中的方法write,把读取到的字节写入到目的地中
5.释放资源
*/
public class CopyFile {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("G:\1.png");
FileOutputStream fos = new FileOutputStream("F:\2.png");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes,0,len);//写入
fos.flush();//刷新
}
//先关闭写的资源
fos.close();
fis.close();
long e = System.currentTimeMillis();
System.out.println("复制文件共耗时:"+(e-s)+"毫秒");
}
}

浙公网安备 33010602011771号