How to copy file in Java?

Here is more Example to explain how to copy file in java.

content of D:/tmp/nourit.txt

IT Training Center for Java and Web technology. 
Almost the whole Examples are easily implemented, 
so that every student or developers can understand, 
all examples are presented in Web with videos.
  • First Example with streams:
package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFileExample1 {

  public static void main(String[] args) {
    try {
      File originalFile = new File("D:/tmp/nourit.txt");
      File copyfile = new File("D:/tmp/CopyNourit.txt");

      InputStream inStream = new FileInputStream(originalFile);
      OutputStream outStream = new FileOutputStream(copyfile);
      byte[] buffer = new byte[1024];
      int length;
      // copy the file content in bytes
      while ((length = inStream.read(buffer)) > 0) {
        outStream.write(buffer, 0, length);
      }
      inStream.close();
      outStream.close();
      System.out.println("copy successful!");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  • Second Example with FileChannel:

FileChannel can be used to copy file in java.

package com.nourit.javaIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class CopyFileExample2 {

  private static FileOutputStream fileOutputStream;
  private static FileInputStream fileInputStream;

  public static void main(String[] args) {
    try {
      File originalFile = new File("D:/tmp/nourit.txt");
      File destfile = new File("D:/tmp/CopyNourit.txt");

      fileInputStream = new FileInputStream(originalFile);
      FileChannel sourceChannel = fileInputStream.getChannel();
      fileOutputStream = new FileOutputStream(destfile);
      FileChannel destChannel = fileOutputStream.getChannel();
      destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
      
      System.out.println("copy successful!");
      sourceChannel.close();
      destChannel.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  • Third Example with FileUtils

FileUtils is from apache Commons IO FileUtils.copyFile(File srcFile, File destFile)

package com.nourit.javaIO;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class CopyFileExample3 {

  public static void main(String[] args) {
    try {
      File source = new File("D:/tmp/nourit.txt");
      File dest = new File("D:/tmp/CopyNourit.txt");
      FileUtils.copyFile(source, dest);
      System.out.println("copy successful!");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
  • Fourth Example with Files.copy(source.toPath(), dest.toPath()) from java.nio.file.Files
package com.nourit.javaIO;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class CopyFileExample4 {
  public static void main(String[] args) {
    try {
      File source = new File("D:/tmp/nourit.txt");
      File dest = new File("D:/tmp/CopyNourit.txt");
      Files.copy(source.toPath(), dest.toPath());
      System.out.println("copy successful!");
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

 

Output:

D:/tmp/nourit.txt and content ist the same.
IT Training Center for Java and Web technology. 
Almost the whole Examples are easily implemented, 
so that every student or developers can understand, 
all examples are presented in Web with videos.

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*