Copy Contents from One to Another File

Copy Contents from One to Another File


Posted in : Java Posted on : March 29, 2012 at 7:19 PM Comments : [ 0 ]

In this tutorial you will learn how to copy file contents from one file to another file

Java File Copy File Contents Example

To Copy a data from the file, read line from one file and write it to the new file as

FileCopy.java

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class FileCopy {
	public static void main(String[] args) {
		try {

			File newFile = new File("D:\\test-file.jsp");
			FileWriter fileWriter = new FileWriter(newFile);
			PrintWriter printWriter = new PrintWriter(fileWriter);

			File file = new File("D:\\registration.jsp");
			FileInputStream fileInputStream = new FileInputStream(file);
			DataInputStream dataInputStream = new DataInputStream(
					fileInputStream);
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(dataInputStream));
			String fileData = "";
			while ((fileData = bufferedReader.readLine()) != null) {
				printWriter.println(fileData);
			}
			fileWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Download Select Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics