Java FileOutputStream Write save(final InputStream in, final File file)

Here you can find the source of save(final InputStream in, final File file)

Description

Saves the contents of an InputStream in a file.

License

Open Source License

Parameter

Parameter Description
in The InputStream to read from. This stream will not be closed when this method returns.
file The file to save to. Will be replaced if it exists, or created if it doesn't.

Exception

Parameter Description
IOException an exception

Declaration

public final static void save(final InputStream in, final File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w  ww .  j  av  a  2  s  .co m*/
 *   This file is part of Skript.
 *
 *  Skript is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Skript 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 Skript.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 * Copyright 2011-2014 Peter G?ttinger
 * 
 */

import java.io.File;

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

public class Main {
    /**
     * Saves the contents of an InputStream in a file.
     * 
     * @param in The InputStream to read from. This stream will not be closed when this method returns.
     * @param file The file to save to. Will be replaced if it exists, or created if it doesn't.
     * @throws IOException
     */
    public final static void save(final InputStream in, final File file) throws IOException {
        file.getParentFile().mkdirs();
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            final byte[] buffer = new byte[16 * 1024];
            int read;
            while ((read = in.read(buffer)) > 0) {
                out.write(buffer, 0, read);
            }
        } finally {
            if (out != null)
                out.close();
        }
    }
}

Related

  1. save(byte[] data, String path)
  2. save(File file, byte[] bytes)
  3. save(File file, byte[] content)
  4. save(File file, byte[] content)
  5. save(File file, final byte[] encoded)
  6. save(InputStream in, File f)
  7. save(String content, String fileName)
  8. save(String data, String encoding, String file)
  9. save(String f, byte[] data)