What is the simplest way to convert a FileOutputStream into FileInputStream (a piece of code would be great)?
This might help you:
http://ostermiller.org/convert_java_outputstream_inputstream.html
This article mentions 3 possibilities:
Just for reference, doing it the other way round (input to output):
A simple solution with Apache Commons IO would be:
IOUtils.copyLarge(InputStream, OutputStream)
or if you just want to copy a file:
FileUtils.copyFile(inFile,outFile);
If you don't want to use Apache Commons IO, here's what the copyLarge
method does:
public static long copyLarge(InputStream input, OutputStream output) throws IOException
{
byte[] buffer = new byte[4096];
long count = 0L;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
I receive an FileOutputStream. What I want is read it.
You certainly can't read from an OutputStream
. I think you mean you want to read from the file being written to by the FileOutputStream
. I don't think you can do that either. The FileOutputStream
doesn't seem to keep a reference to the file being written to.
What you need to do is discover what File
or path (a String
) was passed into the FileOutputStream
and use that same File
or String
to create a new FileInputStream
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With