Java InputStream Copy to File copyStreamToFile(InputStream inputStream, File destFile)

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

Description

copy Stream To File

License

Open Source License

Declaration

public static void copyStreamToFile(InputStream inputStream, File destFile) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors://from  w ww. j  a  v  a  2  s.  c o  m
 *     Jochen Hiller
 *******************************************************************************/

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    public static void copyStreamToFile(InputStream inputStream, File destFile) throws IOException {
        // ensure that parent dirs are created
        File parentDir = destFile.getParentFile();
        if (parentDir != null) {
            parentDir.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(destFile);
        copyStream(inputStream, fos);
        fos.close();
    }

    public static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
        byte[] bytes = new byte[4096];
        int read = inputStream.read(bytes, 0, 4096);
        while (read > 0) {
            outputStream.write(bytes, 0, read);
            read = inputStream.read(bytes, 0, 4096);
        }
    }
}

Related

  1. copyStreamToFile(final File to, final InputStream from)
  2. copyStreamToFile(InputStream from, File to)
  3. copyStreamToFile(InputStream in, File destination)
  4. copyStreamToFile(InputStream in, File out)
  5. copyStreamToFile(InputStream in, File target)
  6. copyStreamToFile(InputStream pInputStream, File pFile)
  7. copyStreamToFile(InputStream source, File target)
  8. copyStreamToFile(InputStream stream, File destFile)
  9. copyStreamToFile(InputStream stream, File destFile, long fileTime)