java IO将一个文件复制到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2520305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:45:05  来源:igfitidea点击:

java IO to copy one File to another

javafilefile-ioiojava-io

提问by Aly

I have two Java.io.File objects file1 and file2. I want to copy the contents from file1 to file2. Is there an standard way to do this without me having to create a method that reads file1 and write to file2

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

采纳答案by Jo?o Silva

No, there is no built-in method to do that. The closest to what you want to accomplish is the transferFrommethod from FileOutputStream, like so:

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

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

And don't forget to handle exceptions and close everything in a finallyblock.

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

回答by Jonathan Feinberg

No. Every long-time Java programmer has their own utility belt that includes such a method. Here's mine.

不是。每个资深 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();
    }
}

回答by Maddy

If you want to be lazy and get away with writing minimal code use

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

FileUtils.copyFile(src, dest)

from Apache IOCommons

来自 Apache IOCommons

回答by Andrejs

Or use Files.copy(file1,file2)from Google's Guava library.

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

回答by Micha? Kosmulski

Since Java 7you can use Files.copy()from Java's standard library.

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

You can create a wrapper method:

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

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

that can be used in the following way:

可以通过以下方式使用:

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

回答by Radu Linu

In Java 7you can use Files.copy()and very important is: Do not forget to close the OutputStream after creating the new file.

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

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