java IO to copy one File to another
Asked Answered
M

6

20

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

Machute answered 26/3, 2010 at 0:0 Comment(2)
See #107270Intervale
for files and strings, you would rather use the Utils classes like FileUtils and StringUtils. They have a wide range of predefined methods to manipulate files and strings. They are included in the Apache Common package which you can add it ot your pom.xmlPersaud
B
32

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

  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 finally block.

Buzzard answered 26/3, 2010 at 0:8 Comment(2)
A more complete (and correct) version of this answer is available here: #107270. Thanks to stackoverflow.com/users/92937/twentymiles for schooling all of us.Amity
for files and strings, you would rather use the Utils classes like FileUtils and StringUtils. They have a wide range of predefined methods to manipulate files and strings. They are included in the Apache Common package which you can add it ot your pom.xmlPersaud
Z
29

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

FileUtils.copyFile(src, dest)

from Apache IOCommons

Zenger answered 26/3, 2010 at 2:43 Comment(1)
I'm a fan of minimal code. Not sure why it's "lazy" to use a utility package. I love StringUtils.Rhyton
B
9

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

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();
    }
}
Boule answered 26/3, 2010 at 0:3 Comment(0)
O
9

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

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");
Otherwhere answered 6/4, 2014 at 20:29 Comment(0)
S
4

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

OutputStream os = new FileOutputStream(targetFile);
Files.copy(Paths.get(sourceFile), os);
os.close();
Selectee answered 30/10, 2017 at 14:34 Comment(0)
T
1

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

Thermoelectric answered 23/3, 2012 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.