Java program to read a text file and write to another file

In this example we are going to develop a Java program to read a text file and write to another file. We will simple read one text file and write to another text file.

Java program to read a text file and write to another file

In this example we are going to develop a Java program to read a text file and write to another file. We will simple read one text file and write to another text file.

Java program to read a text file and write to another file

Java program to read a text file and write to another file - Creating a simple program for reading text file and writing data into another text file

In this tutorial we are going to learn to copy the content from one file to another. Our requirement is to read a text file and then write the content of the text file into another text file. We should be able to use the API that comes with the java.io package for this requirement.

In this tutorial we have a text file and we want to copy the content into another text file from our Java program. In this example program we will use the FileInputStream and FileOutputStream along with the File class of java.io package to achieve the said functionality.

FileInputStream in Java

The FileInputStream and FileOutputStream classes in Java programming language comes with the core Java default library and you don't have to install any extra package. These two classes are present in java.io package. The FileInputStream class is used to work with the byte input stream and the class is used to read data from a file. The could be text file or image or any other binary file. This class is developed for reading streams of raw bytes such as image/text/binary data. Usually for reading characters the FileReader class is used.

The object of FileInputStream is created by passing the object of File with the constructor. Here is example from our program:

inStream = new FileInputStream(inputFile);

And to read the data we have use the read() method of FileInputStream class. Here is example code:

inStream.read(buffer))

Then the read data is finally saved to another file using the FileOutputStream class of Java. 

FileOutputStream in Java

The FileOutputStream class is part of java.io package in Java and this class is used for writing bytes to the output stream. In this example we will use this class we will use the FileOutputStream class to write bytes to text file. This class is designed to support both binary stream and text stream as it writes the data in the bytes format. In out example we have instantiate the FileOutputStream class with following code:

outStream = new FileOutputStream(outFile);

For writing the bytes following is used in our program:

outStream.write(buffer, 0, length);

So, with the help of FileOutputStream class we are able to write the data to another file. 

File class in Java

The file class is used in Java for working with a file or a directory path. This class is used for working with the files and rectories in Java. It provides the methods for working with files and directories. In our example we have used the following function to open the input and output file. Here is the code:

File inputFile = new File("D:\\examples\\input.txt");
File outFile = new File("D:\\examples\\out.txt");

Here is complete code which reads the data from one text file and then write to another text file:


package net.roseindia;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * Example of reading data from one text file
 * and writing the data to another file
 * Web: https://www.roseindia.net
 */

public class ReadTextFileWriteToAnother {

public static void main(String[] args) {
//create input and output stream objects
FileInputStream inStream = null;
FileOutputStream outStream = null;

try {
	//Files objects
	File inputFile = new File("D:\\examples\\input.txt");
	File outFile = new File("D:\\examples\\out.txt");
	
	//Intialize input and output streams
	inStream = new FileInputStream(inputFile);
	outStream = new FileOutputStream(outFile);
	
	//The buffer size for reading data
	byte[] buffer = new byte[1024];

	int length;
	//Copy data to another file
	while ((length = inStream.read(buffer)) > 0) {
		outStream.write(buffer, 0, length);
	}

	// Closing the input/output file streams
	inStream.close();
	outStream.close();

	System.out.println("Written Content to another file.");

} catch (IOException e) {
	e.printStackTrace();
}

}

}

If you run the above code it will copy the content of one text file into another text file. But you should change the path of the files when you run your program on your computer. Here is the code where you can change the input and output file paths:

File inputFile = new File("D:\\examples\\input.txt");
File outFile = new File("D:\\examples\\out.txt");

Here is the screenshot of the program in Eclipse IDE:

Java program to read a text file and write to another file

In this tutorial we have learned to read a text file and then write it to another file in Java. We explained you how to use the FileInputStream and FileOutputStream classes for reading a text file into bytes and then write the bytes to another file. This will you can write content from one text file to another text file in Java.

Here are more related tutorials: