[小练习] 使用FileInputStream和FileOutp

2019-10-22  本文已影响0人  大写K
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Test {
    // 复制文件1的内容到文件2
    public static void main(String[] args) {
        InputStream in;
        OutputStream out;
        String f1 = File.separator + "Users" + File.separator + "kaye" + File.separator + "Desktop" + File.separator + "file1.txt";
        String f2 = File.separator + "Users" + File.separator + "kaye" + File.separator + "Desktop" + File.separator + "file2.txt";
//        File f1 = new File(File.separator + "Users" + File.separator + "kaye" + File.separator + "Desktop" + File.separator + "file1.txt");
//        File f2 = new File(File.separator + "Users" + File.separator + "kaye" + File.separator + "Desktop" + File.separator + "file2.txt");
        try {
            in = new FileInputStream(f1);
            out = new FileOutputStream(f2);
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}
上一篇 下一篇

猜你喜欢

热点阅读