Write a java program to accept a filename and copy content of one to another while copying only copy the alternate word.

 import java.io.*;

import java.util.*;

class Copy

{

File f1,f2;

Copy()

{

Scanner s=new Scanner(System.in);

System.out.print("Enter source path : "); 

String path1=s.nextLine(); //to get source path of file

System.out.print("Enter source file : ");

String file1=s.nextLine(); //to get source file name

System.out.print("Enter destination path : ");

String path2=s.nextLine(); //to get desitanation path

System.out.print("Enter destination file : ");

String file2=s.nextLine(); //to get destination file name

                // File is a class and it's constructor have 2 parameters File(path_of_file , file_name)

f1=new File(path1,file1);   

f2=new File(path2,file2);

}

void display()

{

try

{

                       // FileInputStream is used to read the data from files in byte

FileInputStream fin=new FileInputStream(f2);  

while(true)

{

int i=fin.read();  // read method is use to read single character from file

if(i==-1)   // if there is no more characters in file read method return -1

{

break;

}

System.out.print((char)i);   // to print character in a file

}

fin.close(); // to close the file

}

catch(Exception e)

{

System.out.println(e);

}

}

void copycontent()

{

try

{

FileInputStream fin=new FileInputStream(f1);

                        //  FileOutputStream  is used to write the data from files in byte

FileOutputStream fout=new FileOutputStream(f2);  

while(true)

{

int i=fin.read();

if(i==-1)

{

break;

}

fout.write(i);

}

}

catch(Exception e)

{

System.out.println(e);

}

}

}

class TestCopy

{

public static void main()

{

Copy obj=new Copy();

obj.copycontent();

obj.display();

}

}

0 Comments