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 :
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 making a donation to this charity, thanks.

Related Posts

Popular Posts