Java InputStream to File writeStreamToFile(InputStream is, String fileName)

Here you can find the source of writeStreamToFile(InputStream is, String fileName)

Description

Write a stream's content to a file.

License

Apache License

Parameter

Parameter Description
is a parameter
fileName a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static final void writeStreamToFile(InputStream is, String fileName) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

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

public class Main {
    private static final int BUFFER_LENGTH = 1024;

    /**/*w ww  . j  ava 2  s. c om*/
     * Write a stream's content to a file.
     * @param is
     * @param fileName
     * @throws IOException
     */
    public static final void writeStreamToFile(InputStream is, String fileName) throws IOException {
        //Make sure the dir path is ready
        File file = new File(fileName);
        file.getParentFile().mkdirs();
        FileOutputStream fos = null;
        byte[] buf = new byte[BUFFER_LENGTH];
        try {
            fos = new FileOutputStream(file);
            int len;
            while ((len = is.read(buf, 0, BUFFER_LENGTH)) > -1) {
                fos.write(buf, 0, len);
            }
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}

Related

  1. writeStreamToFile(InputStream inputStream, File targetFile)
  2. writeStreamToFile(InputStream inputStream, String extension)
  3. writeStreamToFile(InputStream is, File file)
  4. writeStreamToFile(InputStream is, File out)
  5. writeStreamToFile(InputStream is, File toFile)
  6. writeStreamToFile(InputStream is, String path)
  7. writeStreamToFile(InputStream skypeFrameworkStream, File skypeFramework)
  8. writeStreamToFile(InputStream stream, File file)
  9. writeStreamToFile(String chemin, String contenu)