Java InputStream Copy to File copyStreamToFile(InputStream source, File target)

Here you can find the source of copyStreamToFile(InputStream source, File target)

Description

Copy an input stream to a file

License

Open Source License

Declaration

public static void copyStreamToFile(InputStream source, File target) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w  w w .  ja  va 2  s . 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 {
    /** Copy an input stream to a file */
    public static void copyStreamToFile(InputStream source, File target) throws IOException {
        FileOutputStream outStream = new FileOutputStream(target);
        copyInputStream(source, outStream);
    }

    /** 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. copyStreamToFile(InputStream in, File destination)
  2. copyStreamToFile(InputStream in, File out)
  3. copyStreamToFile(InputStream in, File target)
  4. copyStreamToFile(InputStream inputStream, File destFile)
  5. copyStreamToFile(InputStream pInputStream, File pFile)
  6. copyStreamToFile(InputStream stream, File destFile)
  7. copyStreamToFile(InputStream stream, File destFile, long fileTime)
  8. copyStreamToFile(InputStream stream, File file)