利用IO实现文件复制

import java.io.*;

/**
 * 利用IO实现文件复制
 */
public class FileCopy {

    public static void main(String[] args) {
        File source = new File("D:\\source.jpg");
        File target = new File("D:\\target.jpg");
        try(
                FileInputStream fileInputStream = new FileInputStream(source);
                FileOutputStream fileOutputStream = new FileOutputStream(target);
        ) {
            byte[] buffer = new byte[1024];
            int byteRead;
            while ((byteRead = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, byteRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37732829/article/details/104327697