java IO复制文件

package com.tedu.day1201;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class CopyFile {
    public static void main(String[] args) {
        FileInputStream in= null;
        FileOutputStream out =null;
        String source = "test.txt";
        String tar = "test3.txt";
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(tar);
            int data;
            while ((data=in.read())!=-1){
                out.write(data);
            }
            in.close();
            out.close();
            System.out.println("文件复制陈宫了");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close(); // 可能出现IOException
                out.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
posted @ 2022-11-05 20:02  竹石2020  阅读(12)  评论(0编辑  收藏  举报