FileOutputStream将从一个文件中读取的内容写到另一个文件中

 package com.janson.day2018082

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

public class TestFileOutputStream {
    public static void main(String[] args) {
        FileInputStream in = null;
        FileOutputStream out = null;
        int f = 0;
        int num = 0;
        try{
            in = new FileInputStream(System.getProperty("user.dir") + "/src/com/janson/day20180827/TestFileInputStream.java");
            out = new FileOutputStream("D:/javaExample/file/TestIO.java");
        }catch (FileNotFoundException e) {
            System.out.println("找不到相应文件");
        }

        try {
            while ((f = in.read()) != -1) {
                out.write(f);
                num ++;
            }
            out.flush();
out.close(); System.out.println("写入文件成功,共写入了" + num + "个字节"); }catch (IOException e) { System.out.println("读取文件出错了"); } } }

 

posted @ 2018-09-07 16:15  那心之所向  阅读(482)  评论(0编辑  收藏  举报