You need to create new file and copy contents from InputStream to that file:
File file = //...
try(OutputStream outputStream = new FileOutputStream(file)){
IOUtils.copy(inputStream, outputStream);
} catch (FileNotFoundException e) {
// handle exception here
} catch (IOException e) {
// handle exception here
}
I am using convenient IOUtils.copy() to avoid manual copying of streams. Also it has built-in buffering.
Top answer 1 of 8
133
You need to create new file and copy contents from InputStream to that file:
File file = //...
try(OutputStream outputStream = new FileOutputStream(file)){
IOUtils.copy(inputStream, outputStream);
} catch (FileNotFoundException e) {
// handle exception here
} catch (IOException e) {
// handle exception here
}
I am using convenient IOUtils.copy() to avoid manual copying of streams. Also it has built-in buffering.
2 of 8
99
In one line :
FileUtils.copyInputStreamToFile(inputStream, file);
(org.apache.commons.io)
Oracle
docs.oracle.com โบ javase โบ 8 โบ docs โบ api โบ java โบ io โบ FileInputStream.html
FileInputStream (Java Platform SE 8 )
April 21, 2026 - Javaโข Platform Standard Ed. 8 ... A FileInputStream obtains input bytes from a file in a file system.
Videos
09:56
Java Tutorial #75 - Java File Input Stream Class Examples (File ...
09:29
File IO in Java with Input and Output Streams and Writers - YouTube
10:23
Learn Java Programming - FileInputStream Tutorial - YouTube
22:28
Java IO - Input Streams [#3] - YouTube
08:15
Using FileWriter and FileInputStream in Java - YouTube
File Reading in Java using FileReader and FileInputStream
Baeldung
baeldung.com โบ home โบ java โบ java io โบ java โ convert file to inputstream
Java - Convert File to InputStream | Baeldung
January 5, 2024 - @Test public void givenUsingCommonsIO_whenConvertingFileToInputStream_thenCorrect() throws IOException { File initialFile = new File("src/main/resources/sample.txt"); InputStream targetStream = FileUtils.openInputStream(initialFile); } And there we have it. Three simple and clean solutions for opening a stream from a Java file. In this article, we explored various ways to convert a File to InputStream by using different libraries.
W3Schools
w3schools.com โบ java โบ java_fileinputstream.asp
Java FileInputStream
FileInputStream - best for binary data (images, audio, PDFs) or when you need full control of raw bytes. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Baeldung
baeldung.com โบ home โบ java โบ java io โบ java โ write an inputstream to a file
Java - Write an InputStream to a File | Baeldung
August 20, 2025 - How to convert an InputStream to a byte[] using plain Java, Guava or Commons IO. ... @Test public void whenConvertingToFile_thenCorrect() throws IOException { Path path = Paths.get("src/test/resources/sample.txt"); byte[] buffer = java.nio.file.Files.readAllBytes(path); File targetFile = new File("src/test/resources/targetFile.tmp"); OutputStream outStream = new FileOutputStream(targetFile); outStream.write(buffer); IOUtils.closeQuietly(outStream); }
Programiz
programiz.com โบ java-programming โบ examples โบ load-file-as-inputstream
Java Program to Load File as InputStream
We then used the read() method to read all the data from the file. ... We can also load this Java file as input stream. import java.io.InputStream; import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { // file Test.java is loaded as input stream ...
Mkyong
mkyong.com โบ home โบ java โบ how to convert inputstream to file in java
How to convert InputStream to File in Java - Mkyong.com
December 27, 2020 - In Java 9, we can use the new InputStream#transferTo to copy the InputStream to an OutputStream directly.
Coderanch
coderanch.com โบ t โบ 278406 โบ java โบ Writing-input-stream-file
Writing input stream to file (I/O and Streams forum at Coderanch)
February 15, 2007 - I know how to get the InputStream ... the file. Can anyone help me? ... All you need to do is open an output stream (presumably a FileOutputStream in your case), read from the input stream and write to the output stream. Have a look at the Java tutorial chapter on IO...
Medium
medium.com โบ @praveenraovp01 โบ input-streams-in-java-613a953f730c
Input Streams in Java!. A stream in Java can be defined as aโฆ | by PraveenLearnsStuff | Medium
May 6, 2023 - And, the object input stream to read the object from the file. Note: The Dog class implements the Serializable interface. It is because the ObjectOutputStream only writes the serializable objects to the output stream, which we will cover in another article! ... I hope this article gives an idea about the implementation of InputStream in Java 8 and above.
Programiz
programiz.com โบ java-programming โบ fileinputstream
Java FileInputStream (With Examples)
The FileInputStream class of the java.io package can be used to read data (in bytes) from files. It extends the InputStream abstract class.
Coderanch
coderanch.com โบ t โบ 278228 โบ java โบ InputStream-file-absolute-path
how to get InputStream from a file with absolute path? (I/O and Streams forum at Coderanch)
October 12, 2006 - It just keeps giving me null for "is": String absFilePath = "/D:/my_dir/.../sample_file.txt"; InputStream is = getClass().getResourceAsStream(absFilePath); regards, ... I believe you should read the API Javadoc about getResourceAsStream(String name) Now, while you do that, I believe that since you already have the full path to the file it is simpler to say: Does this help?
Top answer 1 of 5
139
InputStream is;
try {
is = new FileInputStream("c://filename");
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
2 of 5
56
InputStream is = new FileInputStream("c://filename");
return is;
Coderunbox
coderunbox.com โบ example โบ example.java โบ load-file-as-inputstream
Java Program to Load File as InputStream | CodeRunBox - Code Example
To understand this example, you should have the knowledge of the following Java programming topics: ... import java.io.InputStream; import java.io.FileInputStream; public class Main { public static void main(String args[]) { try { // file input.txt is loaded as input stream // input.txt file contains: // This is a content of the file input.txt InputStream input = new FileInputStream("input.txt"); System.out.println("Data in the file: "); // Reads the first byte int i = input.read(); while(i != -1) { System.out.print((char)i); // Reads next byte from the file i = input.read(); } input.close(); } catch(Exception e) { e.getStackTrace(); } } }