Android InputStream Save writerFromInputStream(InputStream stream, String path, String fileName)

Here you can find the source of writerFromInputStream(InputStream stream, String path, String fileName)

Description

writer From Input Stream

Declaration

public static void writerFromInputStream(InputStream stream,
        String path, String fileName) throws IOException 

Method Source Code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class Main{
    private static Logger logger = Logger.getLogger(FileUtil.class);
    /*from  w  ww . java 2 s. co  m*/
    public static void writerFromInputStream(InputStream stream,
            String path, String fileName) throws IOException {
        FileOutputStream fs = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            fs = new FileOutputStream(path + File.separator + fileName);
            byte[] buffer = new byte[1024 * 1024];
            int byteread = 0;
            while ((byteread = stream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
                fs.flush();
            }
        } catch (Exception e) {
            logger.error("Write stream error!", e);
        } finally {
            if (fs != null) {
                try {
                    fs.close();
                } catch (Exception e) {
                    logger.warn("Resource FileOutputStream error!", e);
                }
            }
            if (stream != null) {
                try {
                    stream.close();
                } catch (Exception e) {
                    logger.warn("Resource InputStream error!", e);
                }
            }
        }
    }
}

Related

  1. dumpToFile(File file, InputStream inputStream)
  2. saveFile(InputStream in, String fileName)
  3. saveToLocal(InputStream in, String filePath)
  4. writeFile(File outputFile, InputStream inputStream)
  5. saveFile(Context context, String fileName, InputStream inputStream)
  6. writeXmlToTempFile(InputStream xmlStream, String filePath, String closingTag)
  7. writeToString(InputStream stream)