Problem : How you can make copy of a text file?
Solution : java.io.FileReader will be used to read data from source text file and java.io.FileWriter will be used to write data to target text file. Program :Step 1 - CodingCreate a text file c:sunilos/CopyFile.java and copy below contents.import java.io.FileReader; import java.io.FileWriter; import com.sun.corba.se.impl.ior.WireObjectKeyTemplate; /** * A program to copy from one file to another file. */ public class CopyFile { public static void main(String[] args) throws Exception{ String source = "c:/sunilos/sunilos.txt"; String target = "c:/sunilos/sunilos-copy.txt"; //File writer takes chars and convert into bytes and write to a file FileReader reader = new FileReader(source); // PrintWriter takes lines and convert that into charters and gives to FileWriter FileWriter writer = new FileWriter(target); int ch = reader.read(); while(ch!=-1){ writer.write(ch); ch= reader.read(); } writer.close(); reader.close(); System.out.println(source + " is copied to " + target); } } Step 2 - Deployment
Step 3 - Testing
OutputC:\sunilos>java -cp . com.sunilos.io.CopyFilec:/sunilos/sunilos.txt is copied to c:/sunilos/sunilos-copy.txt <<Previous | Next>> |