JavaIO流中实现文件复制

Posted g0rez

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaIO流中实现文件复制相关的知识,希望对你有一定的参考价值。

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

public class FileCopy {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis=new FileInputStream("D:\\\\test.doc");
            fos = new FileOutputStream("D:\\\\testfile\\\\test.doc");
            byte[] bytes = new byte[1024];
            int redCount=0;
            while((redCount=fis.read(bytes)) != -1){
                fos.write(bytes,0,redCount);
            }
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

以上是关于JavaIO流中实现文件复制的主要内容,如果未能解决你的问题,请参考以下文章