Java InputStream Copy to File copyStream(InputStream in, File dest)

Here you can find the source of copyStream(InputStream in, File dest)

Description

Copy a file content to another location.

License

Open Source License

Parameter

Parameter Description
in the input stream.
dest the destination.

Return

true if the file was successfully copied, false otherwise.

Declaration

public static boolean copyStream(InputStream in, File dest) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2009 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
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w w  w . j  a va  2s.c o m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.File;

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

public class Main {
    /**
     * Copy a file content to another location.
     *
     * @param in the input stream.
     * @param dest the destination.
     * @return <code>true</code> if the file was successfully copied,
     *    <code>false</code> otherwise.
     */
    public static boolean copyStream(InputStream in, File dest) {

        try {
            OutputStream out = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related

  1. copyStream(final InputStream in, final File dest)
  2. copyStream(final InputStream is, final File destinationFile)
  3. copyStream(InputStream copyFrom, File copyTo)
  4. copyStream(InputStream in, File dest)
  5. copyStreamIntoFile(File outFile, InputStream is)
  6. copyStreamsToFile(String path, Map streamMap)
  7. copyStreamsToFolder(Iterator> streams, File folder)
  8. copyStreamToFile(final File to, final InputStream from)