Sample Project - Copying a File
Start course
1h 46m

In this course, we'll learn about the Java Input/Output subject.

Learning Objectives

  • API and Java I/O
  • OutputStream Class
  • InputStream Class
  • Reader Class
  • Writer Class
  • Serialization, Deserialization, and Non-Serializable objects

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this lesson, we'll create a copy of a file using the FileInputStream and FileOutputStream classes. With the FileInputStream class, we'll read the data in the source file in bytes with the help of the read() method. And we'll write each byte we read to the target file using the write() method of the FileOutputStream class. When this process is completed, a copy of the source file will be created. If you're ready, let's start writing the necessary codes. First of all, I want to show you the file that I will create for the copy. The file I will copy is the original.txt file. This file is in the Java IO folder in the My Documents folder on My Computer. You can create a similar file in any folder on your computer. Thus, you'll learn how to read data from a file in a different directory and how to write data to a file in a different directory. Let's take a look inside the file. I've included some draft text in this file as an example. You can use a ready-made draft text as we did in the previous lessons, or you can create your own text. Now, let's switch to Eclipse and start making a copy of this file.

First, I'll create a new project. I click on the 'File' menu and select the 'New', 'Java Project' option.

The project name can be CopyingFile, and the Java version will be Java 8. And, I'll click the 'Finish' button. Now, I'll create a new class in this project. I right-click on the 'src' folder and select the 'New', 'Class' option. The package name can be copyingFile. The class name can be CopyFile, and I'll check the checkbox for the main() method and click the 'Finish' button. First, I'll create an object from the FileInputStream class. FileInputStream fileInputStream = new FileInputStream(). In parenthesis, I have to write the file name. If the file I'll be processing was in this project, it would be sufficient to write the file name directly with its extension. But this time, the file is in a different folder. Therefore, we must write the file path here. Now, let's get the file path of the file we're going to copy. You can get the file path by clicking the address bar just above, and you can copy the path from here. Or, right-click on the file, click 'Properties' from the pop-up menu. Here, you can find the path file in the Location section. Let's copy it now. I go back to the Eclipse again. And let's paste this file in the parentheses between double quotes. Finally, we have to write the file name with its extension. After the double backslash, I write the file name, the original.txt file.

I will add throws declaration to the main() method. Now, we'll create an object from the FileOutputStream class. FileOutputStream fileOutputStream = new FileOutputStream(). In parentheses, I should write the path of the file in which the copy to be created will be saved. If you want, let's save it in the same directory. Of course, you can save it in a different folder if you want. Also, let's get the file name from the user. Let the user specify the file name to be created. So, I'm going to create a scanner object right here. Scanner scanner = new Scanner( Now, let's give a message to the user. System.out.println("Please enter the file name:"). Now, let's transfer the file name specified by the user to a string. String fileName = scanner.nextLine(). Now, let's add this fileName to the end of the file path. Of course, there must be a double backslash again. We must also specify the extension of this file. Let the extension be txt again. Now, we can start the copying process. First, let's create a variable of type int; int dataRead. Now, let's read the data in the source file with the do-while loop. First, let's transfer the integer value returned by the read() method of the FileInputStream class to the dataRead variable. dataRead = Now, let's check if the read operation is finished.

If you remember, the read() method was returning the -1 value when there was no data to read. So, we can create an if statement here. If the dataRead is not equal to the -1, in this case, we can write the read byte to the new file. fileOutputStream.write(dataRead). So, as soon as each byte is read, it will be written to the new file. Now, let's create the while condition. The condition here will be the same as the if condition. So, I'm copying and pasting this condition here. So, this loop will continue until all the data in the source file is read. The data read in each loop will be written to the new file. The loop will end when all the data is read. Let's show a message before and after the loop that indicates the copying process has started and finished. Just before the loop, System.out.printIn("The copying process has started...") Now, let's create the message that indicates the copying process is finished, right after the loop. System.out.println("Copying is complete."). We can finally close the stream. fileInputStream.close(). fileOutputStream.close(). scanner.close(). Everything is ready. If you want, let's run the program and test it.

Let's enter a file name. For example, the file name can be new file. When you press 'Enter', the message that copying starts and ends appeared on the console. The copying process was completed very quickly. Now, let's check the folder where the file is located. And as you can see, the new file has been created. If we look at the contents of the original and duplicate files, we can see that they both have the same content. That's how we learn to make a copy of a file using the FileInputStream and FileOutputStream classes. You can also do this using the Reader and Writer classes. I think the logic of making copies is understood. Let's take a short break here. See you in the next lesson.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics