Copying File

java 实现拷贝文件的例子(从网上摘来的)。

This example uses file streams to copy the contents of one file to another file. See e172 Copying One File to Another for an example that uses file channels.

    // Copies src file to dst file.
    // If the dst file does not exist, it is created
    void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
   
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
调用时:copy(new File("D://temp//1//1.txt"), new File("D://temp//2//1.txt"));

阅读更多
文章标签: file dst byte java
个人分类: java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Copying File

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭