Sample Programs

Sample java program to copy file

 package org.jusfortechies.example; 

 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;

 public class CopyFile {

   public static void main(String[] args) {

     try { 
			
       String fromFile = "C:\\from.txt";
       String toFile = "C:\\to.txt";

       File inputFile = new File(fromFile);
       File outputFile = new File(toFile);

       FileReader in = new FileReader(inputFile);
       FileWriter out = new FileWriter(outputFile);
       int i;

       while ((i = in.read()) != -1)
          out.write(i);

       in.close();
       out.close();

     } catch (Exception e) {
       System.err.println("File cannot be copied : " + e);
     }
   }

 }




blog comments powered by Disqus