Java copying file with Guava



Google Guava is an open-source set of common libraries for Java. It includes Files.copy() for copying a file.

We need this Guava dependency:
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>22.0</version>
</dependency>

Java copying file with Guava

The example copies a file with Guava's Files.copy().
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;

public class CopyFileGuava {

    public static void main(String[] args) throws IOException {

        File source = new File("src/main/resources/bugs.txt");
        File dest = new File("src/main/resources/bugs2.txt");

        Files.copy(source, dest);
    }
}

Comments