Java InputStream to OutputStream copyStreamToFile(InputStream stream, String outputFilePath)

Here you can find the source of copyStreamToFile(InputStream stream, String outputFilePath)

Description

Uses the default charset

License

Apache License

Parameter

Parameter Description
stream a parameter
outputFilePath a parameter

Exception

Parameter Description
FileNotFoundException if the output file path is invalid
IOException an exception

Declaration

public static void copyStreamToFile(InputStream stream, String outputFilePath)
        throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*//from  www . j av a2s .  c o  m
 * Copyright 2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

public class Main {
    /**
     * Uses the default charset
     * @param stream
     * @param outputFilePath
     * @throws FileNotFoundException if the output file path is invalid
     * @throws IOException
     */
    public static void copyStreamToFile(InputStream stream, String outputFilePath)
            throws FileNotFoundException, IOException {
        BufferedInputStream is = new BufferedInputStream(stream);
        OutputStream os = null;
        try {
            os = new FileOutputStream(new File(outputFilePath));
            byte[] b = new byte[1024];
            int read;
            while ((read = is.read(b)) != -1) {
                os.write(b, 0, read);
            }
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    // ignore
                }
            }
            try {
                is.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

Related

  1. copyStreamToFile(final InputStream stream, final File output)
  2. copyStreamToFile(InputStream input, String outputPath)
  3. CopyStreamToFile(InputStream inputStream, File outputFile)
  4. copyStreamToFile(InputStream inputStream, File outputFile)
  5. copyStreamToFile(InputStream is, File outputFile)
  6. copyStreamToFileBinary(final InputStream stream, final File output)
  7. copyStreamToStream(final InputStream input, final OutputStream output)
  8. copyStreamToStream(InputStream in, OutputStream out)
  9. inputStreamToOutputStream(final InputStream in, final OutputStream out)