Java FileInputStream Copy CopyFileToDirectory(String file_name, String from_directory, String to_directory)

Here you can find the source of CopyFileToDirectory(String file_name, String from_directory, String to_directory)

Description

Copy File To Directory

License

Apache License

Declaration

public static void CopyFileToDirectory(String file_name, String from_directory, String to_directory) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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 Main {
    public static void CopyFileToDirectory(String file_name, String from_directory, String to_directory) {
        InputStream inStream = null;
        OutputStream outStream = null;

        try {//w  w  w .j  av a  2s . c  o m

            File afile = new File(from_directory + "/" + file_name);
            File bfile = new File(to_directory + "/" + file_name);

            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);

            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("File is copied successful!");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Related

  1. copyFileToContainer(File pChild, IContainer pContainer)
  2. copyFileToDestDir(String srcFilePath, String destFileDir)
  3. copyFileToDir(File oldFile, File outputDir)
  4. copyFileToDir(File sourceFile, File destDir)
  5. copyFileToDir(String targetDir, String[] filePath)
  6. copyFileToFile(File file, File destFile, boolean overwrite)
  7. copyFileToFile(File input, String outputPath, String exportName)
  8. copyFileToOutputStream(File file, OutputStream stream)
  9. copyFileToOutputStream(String fileLocation, OutputStream os)