Java FileInputStream Copy copyFile(File inputFile, OutputStream os)

Here you can find the source of copyFile(File inputFile, OutputStream os)

Description

Utility method to copy a file into an java.io.OutputStream .

License

Open Source License

Parameter

Parameter Description
inputFile the file to copy
os the stream to copy into

Exception

Parameter Description
IOException when an error occurs during the copy.

Declaration

private static void copyFile(File inputFile, OutputStream os) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2007(c) G?nome Qu?bec. All rights reserved.
 * // w  w  w  .  j a v  a2s . co m
 * This file is part of GenoByte.
 * 
 * GenoByte is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * GenoByte is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *******************************************************************************/

import java.io.File;
import java.io.FileInputStream;

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

public class Main {
    /**
     * Utility method to copy a file into an {@link java.io.OutputStream}.
     * 
     * @param inputFile the file to copy
     * @param os the stream to copy into
     * @throws IOException when an error occurs during the copy.
     */
    private static void copyFile(File inputFile, OutputStream os) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(inputFile);
            // A 1Mb data buffer
            byte[] buffer = new byte[1024 * 1024];
            int n = 0;
            while ((n = fis.read(buffer)) != -1) {
                os.write(buffer, 0, n);
            }
        } finally {
            if (fis != null)
                fis.close();
        }
    }
}

Related

  1. copyFile(File inFile, File outFile)
  2. copyFile(File input, File output)
  3. copyFile(File inputFile, File outputFile)
  4. copyFile(File inputFile, File outputFile)
  5. copyFile(File inputFile, File outputFile)
  6. copyFile(File of, File nf)
  7. copyFile(File oldFile, File newFile)
  8. copyFile(File oldFile, String newPath)
  9. copyFile(File orig, File dest)