美文网首页
[小练习] 使用FileInputStream和FileOutp

[小练习] 使用FileInputStream和FileOutp

作者: 大写K | 来源:发表于2019-10-22 17:41 被阅读0次
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();
        } 
    }
}

相关文章

网友评论

      本文标题:[小练习] 使用FileInputStream和FileOutp

      本文链接:https://www.haomeiwen.com/subject/xcqcvctx.html