如何从复制文件中的原始文件复制文本?

时间:2018-06-27 07:13:13

标签: java-io

我制作了此程序,以便使用java.io复制包含文本的文件。

如何传输它,以便将文本从原始文件复制到复制文件?

包com.java;

import java.io.*;

public class CopyFile {

    public static void main(String[] args) throws IOException{

        PrintStream pr = System.out;

        int i;

        File f,a = null;

        FileInputStream fin = null;

        FileOutputStream fout = null;

        try {
             f = new File("D:\original.txt");//original file
             a = new File("D:\copy.txt");//copy file
             fin = new FileInputStream(f);//transfer of the original file to the iput stream

             fout = new FileOutputStream(a);//transfer of the copy file to the output stream

            do{
                i = fin.read();//reading original file
                if(i != -1) fout.write(i);//transfer of the copy file
            } while (i != -1);
        } catch (IOException e ) {
            pr.println("error, iput-output" + e);
        } finally {
            try {
                if(fin != null) fin.close();
            }
            catch (IOException e) {
                pr.println("error, cannot closed input stream" + e);
            }
            try {
                if(fout !=null) fout.close();
            }
            catch (IOException e) {
                pr.println("error, cannot closed output stream" + e);
            }
        }
    }
}

0 个答案:

没有答案