Program to Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program 1  to copy file using FileInputStream and FileOutputStream >

java.io.File does not provide any direct method to copy a file.



We must follow these steps to copy a file>
  1. Read file using FileInputStream - data of file will be converted in byte and then
  2. write those bytes in another file using FileOutputStream.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class CopyFile {
   public static void main(String... args) {
          String file ="c:/myFile.txt";
          String fileCopy= "c:/myFileCopy.txt";
         
          FileInputStream fis= null;
          FileOutputStream fos = null;
          try {
                 fis = new FileInputStream(file);
                 fos = new FileOutputStream(fileCopy);
                 byte[] b = new byte[1024];
                 int ch;
                 while ((ch = fis.read(b)) != -1) { //read bytes from myFile.txt
                       fos.write(b, 0, ch); //write bytes in myFileCopy.txt
                 }
                 fos.flush(); // bytes in buffer are written in file
                
                 System.out.println(file + " has been successfully copied to " + fileCopy);
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (fis != null){
                              fis.close(); //close FileInputStream
                       }
                       if (fos != null){
                              fos.close(); //close FileOutputStream
                       }
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/* OUTPUT
c:/myFile.txt has been successfully copied to c:/myFileCopy.txt
*/



Program 2  to copy file using org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory) method >


org.apache.commons.io.FileUtils's copyFile(file, fileCopy) method can be used for copying file.


import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/** JavaMadeSoEasy.com */
public class CopyFile {
   public static void main(String... args) {
          String fileName = "c:/myFile.txt";
          String fileCopyName = "c:/myFileCopy.txt";
          File file = new File(fileName);
          File fileCopy = new File(fileCopyName);
          try {
                 FileUtils.copyFile(file, fileCopy);
                 System.out.println(fileName + " copied successfully to "+ fileCopyName);
          } catch (IOException e) {
                 e.printStackTrace();
          }
   }
}
/* OUTPUT
c:/myFile.txt copied successfully to c:/myFileCopy.txt
*/


RELATED LINKS>

Create File using createNewFile() method in java file IO


Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program to Move a file - in java file IO

Program to Rename a file - in java file IO

Program to Find file is Read only or not in java 7



Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


Program to Identify File or Directory - in java file IO | And in java 7 using java.nio.file



eEdit
Must read for you :