Java FileInputStream Copy copyFileToOutputStream(File file, OutputStream stream)

Here you can find the source of copyFileToOutputStream(File file, OutputStream stream)

Description

Write a file to an output stream

License

Open Source License

Declaration

public static void copyFileToOutputStream(File file, OutputStream stream) throws IOException 

Method Source Code

//package com.java2s;
/**// w  w w  .  j a v a 2s . c  o  m
 * e-Science Central
 * Copyright (C) 2008-2013 School of Computing Science, Newcastle University
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation at:
 * http://www.gnu.org/licenses/gpl-2.0.html
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
 */

import java.io.*;

public class Main {
    /** Write a file to an output stream */
    public static void copyFileToOutputStream(File file, OutputStream stream) throws IOException {
        copyInputStream(new FileInputStream(file), stream);
    }

    /** Copy the data from one stream to another */
    public static final void copyInputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[4096];
        int len;

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        out.close();
        in.close();
    }
}

Related

  1. copyFileToDir(File sourceFile, File destDir)
  2. copyFileToDir(String targetDir, String[] filePath)
  3. CopyFileToDirectory(String file_name, String from_directory, String to_directory)
  4. copyFileToFile(File file, File destFile, boolean overwrite)
  5. copyFileToFile(File input, String outputPath, String exportName)
  6. copyFileToOutputStream(String fileLocation, OutputStream os)
  7. copyFileToStream(File currentFile, OutputStream outputStream)
  8. copyFileToStream(File file, OutputStream stream)
  9. copyFileToStream(File file, OutputStream stream)