What is the simplest way to convert a FileOutputStream into FileInputStream (a piece of code would be great)?
解决方案
This might help you:
This article mentions 3 possibilities:
write the complete output into a byte array then read it again
use pipes
use a circular byte buffer (part of a library hosted on that page)
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;
}