问题描述
什么是将FileOutputStream转换为FileInputStream最简单的方法(一段代码会很好)?可能会帮助你:
这篇文章提到了3种可能:
- 将完整的输出写入一个字节数组然后再读取它
- 使用管道
- 使用循环字节缓冲区(在该页面托管的库的一部分) )
仅供参考,反之亦然(输入到输出):
Apache Commons IO的一个简单解决方案是:
IOUtils.copyLarge(InputStream,OutputStream)
或者如果您只想复制文件:
FileUtils.copyFile(inFil即,不过outFile);
如果你不想使用Apache Commons IO,下面是 copyLarge
方法:
pre $ 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;
}
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:
- 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;
}
这篇关于FileOutputStream转换成FileInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!