Java FileOutputStream Create writeFileFromStream(File tempFile, InputStream in)

Here you can find the source of writeFileFromStream(File tempFile, InputStream in)

Description

write File From Stream

License

Open Source License

Declaration

public static int writeFileFromStream(File tempFile, InputStream in) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2010 Oracle//from  w  w w . j  a v  a  2s .  c o m
 * 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
 *
 * Original file was copied from
 * org.eclipse.wst.common.project.facet.core.util.internal.FileUtil
 *
 ******************************************************************************/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

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

public class Main {
    public static int writeFileFromStream(File tempFile, InputStream in) throws IOException {
        byte[] buffer = new byte[1024];

        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempFile));
        BufferedInputStream bin = new BufferedInputStream(in);

        int bytesRead = 0;
        int bytesTotal = 0;

        // Keep reading from the file while there is any content
        // when the end of the stream has been reached, -1 is returned
        while ((bytesRead = bin.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            bytesTotal += bytesRead;
        }

        if (bin != null) {
            bin.close();
        }

        if (out != null) {
            out.flush();
            out.close();
        }

        return bytesTotal;
    }
}

Related

  1. writeFileContent(String sFileName, byte[] content)
  2. writeFileCover(String path, String content)
  3. writeFileData(File dataFile, String targetFile)
  4. writeFileData(File file, byte[] fileData)
  5. writeFileFromBytes(byte[] bytes, File file)
  6. writeFileFromString(File outputFileName, String content)
  7. writeFileRaw(String fileName, byte[][] contents)
  8. writeFileToByte(byte[] bytes, File file)
  9. writeFileToDisk(ByteArrayOutputStream is, String savePath)