How to convert InputStream to File in Java

In this tutorial, we show you how to convert “File to InputStream” and “InputStream to File“.

1. Convert File to InputStream

Read a file into File object, how to convert it into “input stream” for further process?

	File file = new File("/Users/mkyong/Downloads/file.js");

To convert it, use FileInputStream

	File file = new File("/Users/mkyong/Downloads/file.js");
	InputStream inputStream = new FileInputStream(file);
 
	// or
	InputStream inputStream = 
	      new FileInputStream("/Users/mkyong/Downloads/file.js");

Read a full example

FileToInputStreamApp.java
package com.mkyong.core;
 
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
public class FileToInputStreamApp {
    public static void main(String[] args) {
 
	InputStream inputStream = null;
	BufferedReader br = null;
 
	try {
		// read this file into InputStream
		inputStream = new FileInputStream("/Users/mkyong/Downloads/file.js");
 
		br = new BufferedReader(new InputStreamReader(inputStream));
 
		StringBuilder sb = new StringBuilder();
 
		String line;
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
 
		System.out.println(sb.toString());
		System.out.println("\nDone!");
 
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (inputStream != null) {
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (br != null) {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
    }
}

2. Convert InputStream to File

Not a conversion, but it shows you how to read “input Stream” and write it into another new file via FileOutputStream

InputStreamToFileApp.java
package com.mkyong.core;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
public class InputStreamToFileApp {
    public static void main(String[] args) {
 
	InputStream inputStream = null;
	OutputStream outputStream = null;
 
	try {
		// read this file into InputStream
		inputStream = new FileInputStream("/Users/mkyong/Downloads/holder.js");
 
		// write the inputStream to a FileOutputStream
		outputStream = 
                    new FileOutputStream(new File("/Users/mkyong/Downloads/holder-new.js"));
 
		int read = 0;
		byte[] bytes = new byte[1024];
 
		while ((read = inputStream.read(bytes)) != -1) {
			outputStream.write(bytes, 0, read);
		}
 
		System.out.println("Done!");
 
	} catch (IOException e) {
		e.printStackTrace();
	} 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();
			}
 
		}
	}
    }
}

References

  1. FileInputStream JavaDoc
  2. FileOutputStream JavaDoc
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Bhart

    Thanks! But this is not working for XLSX Spreadsheet. Can you please suggest how to save the spreadsheet to file system after importing?

  • Gerardo

    Dude, your code save my life lol, Greetings.

  • http://kimberlygrnx.wetpaint.com/page/Mhappyboy+Forms+Coupons+-+Find+Coupons+Here Mhappyboy Forms Coupons

    Hello there, just became alert to your blog through Google, and
    found that it’s really informative. I am gonna watch out for brussels. I will appreciate if you continue this in future. Numerous people will be benefited from your writing. Cheers!

  • Ravi.S

    Thats great, thank you very much!

  • http://www.itedvo.eu ITEDVO

    Thats great, thank you very much!

    I need that because I use ini4j to read / write ini-files.
    The constructor needs a File-object but from URL i get an InputStream Object ^^

    Thank you very much, with your solution it works fine!

  • shinigami

    YOU RE GREAT!!!!!

  • Angel

    Omg, this is amazing;
    You helped me like 4 times with spring,springmvc and maven, and now this;
    I admire you;
    thank you really

  • jeffdav

    There is a bug in this code. If the input stream contains less that 1024 bytes the resulting file will always be 1024 bytes. This may not matter, but if, for example, you want to take the MD5 hash of the file it will be wrong, etc.

    Instead do something like:

    int bytesWritten = 0;
    int byteCount = 0;
    byte[] bytes = new byte[1024];
    while ((byteCount = input.read(bytes)) != -1) {
    output.write(bytes, bytesWritten, byteCount);
    bytesWritten += byteCount;
    }

    • lem

      Sorry to burst your bubbfe jeffdav, but you’re wrong. If you see this piece of code

      while ((read = inputStream.read(bytes)) != -1) {
      		out.write(bytes, 0, read);
      	}

      He stores the number of bytes actually read to the variable “read”, and then passes that number to how many bytes he wants to write. You’ve probably already checked, but if you still have doubts, look at the documentation for InputStream and OutputStream.