Sunday 26 May 2019

Java: Copy file from one location to another


Approach 1: Using Streams.
public static void copyFile(File source, File dest) throws IOException {
        
         if(!source.exists() || !source.isFile()) {
                  System.out.println("Source is not exist or not a file");
                  return;
         }
        
         if(!dest.getParentFile().exists()) {
                  System.out.println("Parent directory do not exist. Creating parent directory");
                  dest.getParentFile().mkdirs();
         }

         try (InputStream inputStream = new FileInputStream(source);
                           OutputStream outputStream = new FileOutputStream(dest);) {

                  byte[] buffer = new byte[1024];
                  int length;
                  while ((length = inputStream.read(buffer)) > 0) {
                           outputStream.write(buffer, 0, length);
                  }
         }
}

App.java
package com.sample.app;

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 App {

 public static void copyFile(File source, File dest) throws IOException {

  if (!source.exists() || !source.isFile()) {
   System.out.println("Source is not exist or not a file");
   return;
  }

  if (!dest.getParentFile().exists()) {
   System.out.println("Parent directory do not exist. Creating parent directory");
   dest.getParentFile().mkdirs();
  }

  try (InputStream inputStream = new FileInputStream(source);
    OutputStream outputStream = new FileOutputStream(dest);) {

   byte[] buffer = new byte[1024];
   int length;
   while ((length = inputStream.read(buffer)) > 0) {
    outputStream.write(buffer, 0, length);
   }
  }
 }

 public static void main(String args[]) throws IOException {
  File source = new File("/Users/krishna/Documents/TechnicalDocuments/go_tutorial.pdf");
  File destination = new File("/Users/krishna/Documents/TechnicalDocuments/Go_copy/sample.pdf");
  copyFile(source, destination);
 }

}

Approach 2: Using Files.copy method
Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);


App.java
package com.sample.app;

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

public class App {

 public static void copyFile(File source, File dest) throws IOException {

  if (!source.exists() || !source.isFile()) {
   System.out.println("Source is not exist or not a file");
   return;
  }

  if (!dest.getParentFile().exists()) {
   System.out.println("Parent directory do not exist. Creating parent directory");
   dest.getParentFile().mkdirs();
  }

  Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
 }

 public static void main(String args[]) throws IOException {
  File source = new File("/Users/krishna/Documents/TechnicalDocuments/Go/go_tutorial.pdf");
  File destination = new File("/Users/krishna/Documents/TechnicalDocuments/Go_copy/sample.pdf");
  copyFile(source, destination);
 }

}

Approach 3: Copy using FileChannel
try (FileInputStream inputStream = new FileInputStream(source);
                  FileOutputStream outputStream = new FileOutputStream(dest);) {
         sourceChannel = inputStream.getChannel();
         destChannel = outputStream.getChannel();
         destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}


App.java
package com.sample.app;

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

public class App {

 public static void copyFile(File source, File dest) throws IOException {

  if (!source.exists() || !source.isFile()) {
   System.out.println("Source is not exist or not a file");
   return;
  }

  if (!dest.getParentFile().exists()) {
   System.out.println("Parent directory do not exist. Creating parent directory");
   dest.getParentFile().mkdirs();
  }

  FileChannel sourceChannel = null;
  FileChannel destChannel = null;

  try (FileInputStream inputStream = new FileInputStream(source);
    FileOutputStream outputStream = new FileOutputStream(dest);) {
   sourceChannel = inputStream.getChannel();
   destChannel = outputStream.getChannel();
   destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
  } finally {
   if (sourceChannel != null)
    sourceChannel.close();

   if (destChannel != null)
    destChannel.close();
  }
 }

 public static void main(String args[]) throws IOException {
  File source = new File("/Users/krishna/Documents/TechnicalDocuments/Go/go_tutorial.pdf");
  File destination = new File("/Users/krishna/Documents/TechnicalDocuments/Go_copy/sample.pdf");
  copyFile(source, destination);
 }

}


You may like



No comments:

Post a Comment