Monday, March 7, 2016

Java Copy File Example

Example


package com.freesamplecode.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFileDemo {
 public static void main(String[] args) throws IOException{
  try {
   
   // source file
   FileInputStream fis = new FileInputStream("D:/ngoprek/FREESAMPLECODE/src/com/freesamplecode/java/io/test.txt");
   
   //destination file
   FileOutputStream fos = new FileOutputStream("D:/ngoprek/FREESAMPLECODE/src/com/freesamplecode/java/io/testcopy.txt");
   byte[] buf = new byte[1024];
   
   int i = 0;
   
   while((i = fis.read(buf)) != -1){
    fos.write(buf, 0, i);
   }
   
   fis.close();
   fos.close();
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
}



0 comments: