Java FileOutputStream Write saveFile(File file, InputStream is)

Here you can find the source of saveFile(File file, InputStream is)

Description

save File

License

Open Source License

Declaration

public static void saveFile(File file, InputStream is) 

Method Source Code


//package com.java2s;
/*//w w w.  ja  v a 2s. c  om
 * Copyright 2010 Torkjel Hongve (torkjelh@conduct.no)
 *
 * This file is part of Upside.
 *
 * Upside 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.
 *
 * Upside 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 Upside.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedOutputStream;
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 saveFile(File file, InputStream is) {
        OutputStream os;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        pipe(is, os);
    }

    public static void pipe(InputStream in, OutputStream out) {
        byte[] data = new byte[4096];
        try {
            int len;
            while ((len = in.read(data)) != -1)
                out.write(data, 0, len);
            out.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. saveFile(byte[] fileData, String outputDir, String fileName)
  2. saveFile(File f, InputStream stream)
  3. saveFile(File f, String fileName, File desDir)
  4. saveFile(File f, String fileName, File desDir)
  5. saveFile(File file, byte[] contenido)
  6. saveFile(File file, String fileName, String filesDirectory)
  7. saveFile(File file, String savePath)
  8. saveFile(final byte[] bytes, final File target)
  9. saveFile(final File file, final byte[] dataToSave)