In this tutorial we will see how to write java program to read a text file and write to another file . We will write a program in java to copy the text content of one file into another file using FileInputStream and
FileOutputStream or BufferedReader and BufferedWriter in this tutorial . Let’s see different program to copy one file to another in java .
- Using FileInputStream and FileOutputStream
- Using BufferedReader and BufferedWriter
Copy one file to another Using FileInputStream and FileOutputStream
We will read content of file using read() method FileInputStream and write the read content into another file using write() method of FileOutputStream .
package com.demo.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyExample {
public static void main(String[] args) {
FileInputStream instream = null;
FileOutputStream outstream = null;
try {
File infile = new File("C://java_examples//input.txt");
File outfile = new File("C://java_examples//output.txt");
instream = new FileInputStream(infile);
outstream = new FileOutputStream(outfile);
byte[] buffer = new byte[1024];
int length;
/*
* copying the contents from input stream to output stream using
* read and write methods
*/
while ((length = instream.read(buffer)) > 0) {
outstream.write(buffer, 0, length);
}
// Closing the input stream
instream.close();
// Closing the output stream
outstream.close();
System.out.println("File copied successfully!!");
} catch (IOException e) {
//print Exception
e.printStackTrace();
}
}
}
Output :
File copied successfully!!
Read method
public int read(byte b[]) throws IOException
This reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
Parameters: buffer b into which the data is read.
Returns: it is return total number of bytes read into the buffer, otherwise -1 if there is no more data because the end of the file has been reached.
It is throws IOException if an I/O error occurs.
Write() Method
public void write(byte b[], int off, int len) throws IOException
This method writes len bytes from the specified byte array starting at offset off to this file output stream.
Copy one file to another Using Using BufferedReader and BufferedWriter
We read content of file using readLine() method of BufferedReader . We will write copy content into another file using write() method of BufferedWriter.
package com.demo.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class CopyExample2 {
public static void main(String[] args) {
String source = "C://java_examples//input.txt";
String dest = "C://java_examples//output.txt";
try {
File inputfile = new File(source);
FileInputStream fileInputStream = new FileInputStream(inputfile);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(fileInputStream));
FileWriter fileWriter = new FileWriter(dest, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String aLine = null;
while ((aLine = bufferedReader.readLine()) != null) {
// Process each line and add output to Dest.txt file
bufferedWriter.write(aLine);
bufferedWriter.newLine();
}
// do not forget to close the buffer reader
bufferedReader.close();
// close buffer writer
bufferedWriter.close();
System.out.println("File copied successfully!!");
} catch (IOException e) {
// print Exception
e.printStackTrace();
}
}
}
readLine() method
public String readLine() throws IOException
Returns a string containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached . it throws IOException If an I/O error occurs .
write() method
public void write(String str) throws IOException
This method use to writes a string. It takes parameter str String to be written and throws:IOException , If an I/O error occurs .
In this tutorial we have learned how to copy the contents of one file to another using java. You can see more file handling java example .