Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write InputStream to a file [duplicate]

I have an object, in this object I have an InputStream which contains a file.

I want to write what is inside of InputStream to a file inside of a folder.

How would I go about doing this in Core Java?

I was able to print out each line of the InputStream using BufferedReader and .readLine(), however I want the entire file wrote to disk not just what is inside of it.

Hopefully this makes sense, thank you.

like image 334
k1308517 Avatar asked Aug 15 '26 10:08

k1308517


1 Answers

If you are using Java 7 or above you can use java.nio.file.Files:

InputStream in = obj.getInputStrem();
Path file = ...;
Files.copy(in, path);

It also supports different options (see CopyOption implementations like StandardCopyOption and LinkOption)

like image 161
vsminkov Avatar answered Aug 17 '26 23:08

vsminkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!