【问题标题】:java IO to copy one File to anotherjava IO将一个文件复制到另一个
【发布时间】:2011-01-31 23:52:43
【问题描述】:

我有两个 Java.io.File 对象 file1 和 file2。我想将文件1的内容复制到文件2。有没有一种标准方法可以做到这一点,而我不必创建一个读取 file1 并写入 file2 的方法

【问题讨论】:

  • 对于文件和字符串,您宁愿使用像 FileUtils 和 StringUtils 这样的 Utils 类。他们有广泛的预定义方法来操作文件和字符串。它们包含在 Apache Common 包中,您可以将其添加到 pom.xml

标签: java file file-io io java-io


【解决方案1】:

如果您想偷懒并摆脱编写最少的代码,请使用

FileUtils.copyFile(src, dest)

来自 Apache IOCommons

【讨论】:

  • 我喜欢极简代码。不知道为什么使用实用程序包是“懒惰的”。我喜欢 StringUtils。
【解决方案2】:

Java 7中你可以使用Files.copy(),非常重要的是:创建新文件后不要忘记关闭OutputStream

OutputStream os = new FileOutputStream(targetFile);
Files.copy(Paths.get(sourceFile), os);
os.close();

【讨论】:

    【解决方案3】:

    Java 7 开始,您可以使用 Java 标准库中的 Files.copy()

    你可以创建一个包装方法:

    public static void copy(String sourcePath, String destinationPath) throws IOException {
        Files.copy(Paths.get(sourcePath), new FileOutputStream(destinationPath));
    }
    

    可以通过以下方式使用:

    copy("source.txt", "dest.txt");
    

    【讨论】:

      【解决方案4】:

      或者使用来自 Google 的 Guava 库的 Files.copy(file1,file2)

      【讨论】:

        【解决方案5】:

        不,没有内置方法可以做到这一点。最接近您想要完成的是来自FileOutputStreamtransferFrom 方法,如下所示:

          FileChannel src = new FileInputStream(file1).getChannel();
          FileChannel dest = new FileOutputStream(file2).getChannel();
          dest.transferFrom(src, 0, src.size());
        

        不要忘记处理异常并关闭 finally 块中的所有内容。

        【讨论】:

        • 此答案的更完整(和正确)版本可在此处获得:stackoverflow.com/questions/106770/…。感谢stackoverflow.com/users/92937/twentymiles 教育我们所有人。
        • 对于文件和字符串,您宁愿使用像 FileUtils 和 StringUtils 这样的 Utils 类。他们有广泛的预定义方法来操作文件和字符串。它们包含在 Apache Common 包中,您可以将其添加到 pom.xml
        【解决方案6】:

        没有。每个长期使用 Java 的程序员都有自己的实用工具带,其中包含这种方法。这是我的。

        public static void copyFileToFile(final File src, final File dest) throws IOException
        {
            copyInputStreamToFile(new FileInputStream(src), dest);
            dest.setLastModified(src.lastModified());
        }
        
        public static void copyInputStreamToFile(final InputStream in, final File dest)
                throws IOException
        {
            copyInputStreamToOutputStream(in, new FileOutputStream(dest));
        }
        
        
        public static void copyInputStreamToOutputStream(final InputStream in,
                final OutputStream out) throws IOException
        {
            try
            {
                try
                {
                    final byte[] buffer = new byte[1024];
                    int n;
                    while ((n = in.read(buffer)) != -1)
                        out.write(buffer, 0, n);
                }
                finally
                {
                    out.close();
                }
            }
            finally
            {
                in.close();
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-10
          • 1970-01-01
          相关资源
          最近更新 更多