Program to copy contents of one file into other file
This program copied the contents of one file into other file suing FileInputStream and FileOutputStream classes. The name of files for reading and writing are passed through command line arguments.
PROGRAM
PROGRAM
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
class CopyFileContentsInOtherFile {
public static void main(String[] args) {
try
{
File f1=new File(args[0]);
File f2=new File(args[1]);
if(f1.exists()==false)
{
System.out.println("Can't Copy Source File Not Exists");
System.exit(0);
}
if(f2.exists()==true)
{
System.out.println("Can't Copy Destination File Exists");
System.exit(0);
}
FileInputStream fis=new FileInputStream(f1);
FileOutputStream fos=new FileOutputStream(f2);
int x;
while((x=fis.read())!=-1) {
fos.write(x);
}
System.out.println("Contents of file '"+f1+"' are copied in '"+f2+"' successful...");
fis.close();
fos.close();
}
catch(Exception ee) {
System.out.println(ee);
}
}
}
OUTPUT
C:\>javac CopyFileContentsInOtherFile.java C:\>java CopyFileContentsInOtherFile abc.txt xyz.txt Contents of file 'abc.txt' are copied in 'xyz.txt' successful...
Comments
Post a Comment