Here you can find the source of copyStreamToFile(InputStream stream, String outputFilePath)
Parameter | Description |
---|---|
stream | a parameter |
outputFilePath | a parameter |
Parameter | Description |
---|---|
FileNotFoundException | if the output file path is invalid |
IOException | an exception |
public static void copyStreamToFile(InputStream stream, String outputFilePath) throws FileNotFoundException, IOException
//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 } } } }