Java File Object Create toFile(InputStream inputStream)

Here you can find the source of toFile(InputStream inputStream)

Description

to File

License

LGPL

Declaration

public static File toFile(InputStream inputStream) throws IOException 

Method Source Code


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

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 File toFile(InputStream inputStream) throws IOException {
        File result = null;/*from ww w  .  j a v a2s  .  c o m*/
        OutputStream outputStream = null;

        try {
            result = File.createTempFile(".tmp", ".tmp");
            //         result = new File("/Users/mkyong/Downloads/holder-new.js");
            // write the inputStream to a FileOutputStream
            outputStream = new FileOutputStream(result);

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    // outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return result;
    }
}

Related

  1. toFile(File dir, String[] path)
  2. toFile(File dstPath, String fullClassName)
  3. toFile(File file, byte[] data)
  4. toFile(IFile file)
  5. toFile(InputStream in, File file)
  6. toFile(JavaFileObject javaFileObject)
  7. toFile(List filenames)
  8. toFile(Map res, String output)
  9. toFile(String base64Code, String targetPath)