Embed presentation
Download to read offline
![Program to copy content of one file to another
existing file
import java.io.*;
public class jonitafilecopy {
public static void main(String[] args)
{
try
{
FileReader fr = new FileReader("pqr.txt");
FileWriter fw = new FileWriter("x.txt");
int k;
while( ( k = fr.read() ) != -1 )
{
fw.write(k);
System.out.print((char) k);
}
fw.close();
fr.close();
}
catch(FileNotFoundException e)
{
System.out.println("File does not exist. " + e);
}
catch(IOException e)
{
System.out.println("Some I/O problem. " + e);
}
}](https://image.slidesharecdn.com/copyingonefiletoanother-131210205838-phpapp02/85/java-copy-file-program-1-320.jpg)

This Java program copies the contents of one text file ("pqr.txt") to another text file ("x.txt"). It uses FileReader and FileWriter to read from the source file and write to the destination file. The program reads the source file character by character and writes each character to the destination file until the end of the source file is reached. Any errors are caught and printed.
![Program to copy content of one file to another
existing file
import java.io.*;
public class jonitafilecopy {
public static void main(String[] args)
{
try
{
FileReader fr = new FileReader("pqr.txt");
FileWriter fw = new FileWriter("x.txt");
int k;
while( ( k = fr.read() ) != -1 )
{
fw.write(k);
System.out.print((char) k);
}
fw.close();
fr.close();
}
catch(FileNotFoundException e)
{
System.out.println("File does not exist. " + e);
}
catch(IOException e)
{
System.out.println("Some I/O problem. " + e);
}
}](https://image.slidesharecdn.com/copyingonefiletoanother-131210205838-phpapp02/85/java-copy-file-program-1-320.jpg)