Java 使用字节流 一次性复制文件

来源:互联网 发布:linux 目录剩余空间 编辑:程序博客网 时间:2024/04/30 04:22
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class testZy {


public static void main(String[] args) throws IOException {
//这段代码放在程序执行前
long begin = System.currentTimeMillis(); 
duqu("G:\\大话数据结构 带书签完整版.pdf","D:\\123\\4r.pdf");//调用duqu方法读出文件目标文件内容

// 这段代码放在程序执行后
long end = System.currentTimeMillis() - begin; 

System.out.println("耗时:" + end + "毫秒");
}


private static void duqu(String path,String path1) throws FileNotFoundException, IOException {
//利用InPutStreamd读取文件内容
FileInputStream fi = new FileInputStream(path);
File file1 = new File(path);//new一个File对象用来获取文档长度

int flen = (int) file1.length();//获取文档长度
byte[] b1 = new byte[flen];//创建一个数组b1,将数组长度设为文档长度
fi.read(b1, 0, flen);//调用带参的read方法,从0开始直到文档长度读取数组b1,
// String s1 = new String(b1);//将b1转换为字符串类型,不必要
// System.out.println(s1);//输出,不必要

File file = new File(path1);//创建要写入的文件夹
file.createNewFile();//创建要写入的文件夹,不必要
FileOutputStream fo = new FileOutputStream(path1); //new一个OutPutStream对象

fo.write(b1);




//关闭流
fi.close();
fo.close();
}
}