Copy Files Using Java Files Class

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to copy a file to another file in Java. There are several ways to copy a file, such as using FileInputStream, FileOutputStream, and the Files class. Here, we will focus on the Files class, which is easier to use because it provides a copy() method that instantly copies the source file to the destination file.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/FileandIOManagementGroup(["`File and I/O Management`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/FileandIOManagementGroup -.-> java/files("`Files`") java/FileandIOManagementGroup -.-> java/io("`IO`") java/FileandIOManagementGroup -.-> java/nio("`NIO`") java/FileandIOManagementGroup -.-> java/create_write_files("`Create/Write Files`") java/FileandIOManagementGroup -.-> java/read_files("`Read Files`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/while_loop("`While Loop`") subgraph Lab Skills java/scope -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/oop -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/packages_api -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/files -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/io -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/nio -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/create_write_files -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/read_files -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/identifier -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/arrays -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/data_types -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/operators -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/strings -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} java/while_loop -.-> lab-117430{{"`Copy Files Using Java Files Class`"}} end

Import required libraries

In the new Java file, import the required libraries using the following code:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.File;
import java.io.FileInputStream;

Create a file object

Create a file object that points to the source file that needs to be copied.

File source = new File("sourceFile.txt");

Create a destination file object

Create a destination file object that points to the file where you want to copy the data from the source file.

File destination = new File("destinationFile.txt");

Use the Files class to copy the data

Use the Files class to copy the data from the source file to the destination file. You can use the copy() method of the Files class to do this.

Path sourcePath = Paths.get(source.getAbsolutePath());
OutputStream destinationStream = new FileOutputStream(destination);
Files.copy(sourcePath, destinationStream);

Use FileInputStream and FileOutputStream to copy the data

In this method, we will use FileInputStream and FileOutputStream to copy the data. Use the following code to read data from the source file and write data using the write() method of the FileOutputStream.

FileInputStream sourceStream = new FileInputStream(source);
FileOutputStream destinationStream = new FileOutputStream(destination);

byte[] buffer = new byte[1024];
int bytesRead;

while ((bytesRead = sourceStream.read(buffer)) > 0) {
    destinationStream.write(buffer, 0, bytesRead);
}

sourceStream.close();
destinationStream.close();

Compile the program

Open a terminal and navigate to the directory ~/project. Compile the program using the following command:

javac CopyFile.java

Execute the program

Run the program using the following command:

java CopyFile

Summary

In this lab, you learned how to copy data from one file to another file using the Files class and FileInputStream and FileOutputStream. The Files class is easier to use because it provides a copy() method that copies data instantly. You can use the FileInputStream and FileOutputStream to copy the data by reading and writing it manually.

Other Java Tutorials you may like