java实现文件复制功能

来源:互联网 发布:印度一夫多妻制 知乎 编辑:程序博客网 时间:2024/05/20 18:48

原理:把原文件读入到输入流里,然后利用输出流写入到新的文件。

代码如下:

/**     * 复制文件     * @param fromFile     * @param toFile     * <br/>     * @throws IOException      */    public void copyFile(File fromFile,File toFile) throws IOException{        FileInputStream ins = new FileInputStream(fromFile);        FileOutputStream out = new FileOutputStream(toFile);        byte[] b = new byte[1024];        int n=0;        while((n=ins.read(b))!=-1){            out.write(b, 0, b.length);        }                ins.close();        out.close();    }


原创粉丝点击