Java 字节流复制文件

思路

先把文件读出来,把文件转换成byte数组,再把byte数组写入目的地;

代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyT {
    public static void main(String[] args) throws IOException {
        //long e = System.currentTimeMillis();
        File file = new File("C:\\Users\\25852\\Pictures\\Camera Roll\\wallhaven-md762m_3840x2160.png");//需要复制的目标文件
        FileInputStream fileIn = new FileInputStream(file);
        FileOutputStream fileOut = new FileOutputStream("D:\\JavaSE\\基础语法\\src\\com\\javaSE\\CommonAPI\\e.png");//复制到哪
        byte[] bytes = new byte[(int)file.length()];//数据变成字节数组
        int i = 0;
        int re = 0;
        while ((re=fileIn.read())!=-1){
            bytes[i++]=(byte)re;
        }
        fileOut.write(bytes);
        //long d = System.currentTimeMillis();//13351
        //System.out.println(d-e);
    }

}
复制代码
分类:
代码人生
标签: