Java FileInputStream Copy copyFileUsingStream(String source_path, String dest_path)

Here you can find the source of copyFileUsingStream(String source_path, String dest_path)

Description

copy File Using Stream

License

Open Source License

Declaration

public static void copyFileUsingStream(String source_path, String dest_path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyFileUsingStream(String source_path, String dest_path) {
        File source = new File(source_path);
        File dest = new File(dest_path);
        InputStream is = null;//from w  w w. j  a va2 s  . c  o m
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            System.out.println(
                    "ERROR: FileUtils: copyFileUsingStream: File not found! exception was: " + e.toString());
        } catch (IOException e) {
            System.out.println(
                    "ERROR: FileUtils: copyFileUsingStream: Error in the I/O! exception was: " + e.toString());
        } finally {
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                System.out.println(
                        "ERROR: FileUtils: copyFileUsingStream: Error while closing the stream! exception was: "
                                + e.toString());
            }
        }
    }
}

Related

  1. copyFileToStream(File file, OutputStream stream)
  2. copyFileToString(java.io.File f)
  3. copyFileToZip(final ZipOutputStream zos, final byte[] readBuffer, final File file, final int bytesInOfZip)
  4. copyFileUsingFileStreams(final File source, final File dest)
  5. copyFileUsingStream(File source, File dest)