Tuesday, 21 October 2014

A program to read text file and copy its content to another.

Reading and writing from files are a basic feature every programmer requires. And in java this feature is done by io classes in the java library. This is a simple java program to read from a txt file amd copy its content to another txt file.

Snippet:

import java.io.*;
public class CopyFile
{
public static void main (String []args)
{
try {
FileWriter fw= new FileWriter("Copy.txt");
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter outFile= new PrintWriter(bw);
FileReader file=new FileReader("names.txt");
BufferedReader br=new BufferedReader(file);
String text;
// read file and output
while (( text = fileInput.readLine())!=null){
outfile.println(text);
}
System.out.println("File successfully copied ");
fileInput.close();
outFile.close();
}
catch(IOException e){
System.err.println(e);
}
}
}

1 comment :