How to Copy a File in Java Program Example

In this example we will copy the content of existing text file into new text file to Create Copy of Text File in JAVA.

Think with me, How it can be done? Three things we have to do to done this job, Firstly we have to create new text file, Secondly we have to Read Existing File and have to Right on New File.

To create a new file First we have to create File object for new object. And then we have to call createNewFile() method which is present in  createNewFile() creates the file.

We have already learned that How to Read a File. To write content on text file in JAVA we have PrintWriter class present in Package. PrintWriter  class contains println() method Which takes a String as an argument and write it on text file as a single line.

The above example was to give you brief understanding about background process of copying any file. Java already provide us libraries to copies file. Here is the example from the java library.