我惊讶地发现,今天,我不能追查任何简单的方法的内容写入InputStream到OutputStreamJava中.显然,字节缓冲区代码并不难写,但我怀疑我只是遗漏了一些会让我的生活更轻松(代码更清晰)的东西.
那么,给定一个InputStream in和一个OutputStream out,是否有更简单的方法来编写以下内容?
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
Run Code Online (Sandbox Code Playgroud)
Mik*_*x6r 392
正如WMR所提到的,org.apache.commons.io.IOUtils从Apache有一个方法copy(InputStream,OutputStream),它正是你正在寻找的.
所以你有了:
InputStream in;
OutputStream out;
IOUtils.copy(in,out);
in.close();
out.close();
Run Code Online (Sandbox Code Playgroud)
...在你的代码中.
你有没有理由避免IOUtils?
use*_*877 321
如果您使用的是Java 7,则文件(在标准库中)是最佳方法:
/* You can get Path from file also: file.toPath() */
Files.copy(InputStream in, Path target)
Files.copy(Path source, OutputStream out)
Run Code Online (Sandbox Code Playgroud)
编辑:当然,从文件创建一个InputStream或OutputStream时它非常有用.用于file.toPath()从文件中获取路径.
要写入现有文件(例如,使用其创建的文件File.createTempFile()),您需要传递REPLACE_EXISTING复制选项(否则FileAlreadyExistsException抛出):
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING)
Run Code Online (Sandbox Code Playgroud)
Ali*_*ani 156
从Java 9开始,InputStream提供了一个transferTo使用以下签名调用的方法:
public long transferTo(OutputStream out) throws IOException
Run Code Online (Sandbox Code Playgroud)
如文档所述,transferTo将:
从此输入流中读取所有字节,并按读取顺序将字节写入给定的输出流.返回时,此输入流将位于流的末尾.此方法不会关闭任一流.
此方法可能会无限期地阻止从输入流中读取或写入输出流.输入和/或输出流异步关闭或传输过程中线程中断的情况下的行为是高度输入和输出流特定的,因此未指定
因此,为了编写Java内容的InputStream一个OutputStream,你可以这样写:
input.transferTo(output);
Run Code Online (Sandbox Code Playgroud)
Mik*_*one 98
我认为这样可行,但一定要测试它......轻微的"改进",但可读性可能有点费用.
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
Run Code Online (Sandbox Code Playgroud)
And*_*ejs 51
使用番石榴ByteStreams.copy():
ByteStreams.copy(inputStream, outputStream);
Run Code Online (Sandbox Code Playgroud)
Jor*_*ise 25
如果您只需InputStream要将其写入a,File那么您可以使用这个简单的函数:
private void copyInputStreamToFile( InputStream in, File file ) {
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
Dil*_*nga 16
PipedInputStream并且PipedOutputStream只应在有多个线程时使用,如Javadoc所述.
另请注意,输入流和输出流不会使用IOExceptions 包装任何线程中断...因此,您应该考虑在代码中加入中断策略:
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您希望使用此API来复制大量数据,或者将数据从流中陷入无法忍受的长时间,这将是一个有用的补充.
Bul*_*aza 14
该JDK所以它好像有没有笨重的第三方库(这可能不做什么不同呢)没有"容易"的方式使用相同的代码.以下内容直接复制自java.nio.file.Files.java:
// buffer size used for reading and writing
private static final int BUFFER_SIZE = 8192;
/**
* Reads all bytes from an input stream and writes them to an output stream.
*/
private static long copy(InputStream source, OutputStream sink)
throws IOException
{
long nread = 0L;
byte[] buf = new byte[BUFFER_SIZE];
int n;
while ((n = source.read(buf)) > 0) {
sink.write(buf, 0, n);
nread += n;
}
return nread;
}
Run Code Online (Sandbox Code Playgroud)
hol*_*s83 13
对于那些使用Spring框架的人来说,有一个有用的StreamUtils类:
StreamUtils.copy(in, out);
Run Code Online (Sandbox Code Playgroud)
以上并没有关闭流.如果要在复制后关闭流,请使用FileCopyUtils类:
FileCopyUtils.copy(in, out);
Run Code Online (Sandbox Code Playgroud)
使用JDK方法无法轻松实现这一点,但正如Apocalisp已经指出的那样,你不是唯一一个有这个想法的人:你可以使用来自Jakarta Commons IO的IOUtils,它还有很多其他有用的东西, IMO实际上应该是JDK的一部分......
使用Java7和try-with-resources,附带简化且可读的版本.
try(InputStream inputStream = new FileInputStream("C:\\mov.mp4");
OutputStream outputStream = new FileOutputStream("D:\\mov.mp4")){
byte[] buffer = new byte[10*1024];
for (int length; (length = inputStream.read(buffer)) != -1; ){
outputStream.write(buffer, 0, length);
}
}catch (FileNotFoundException exception){
exception.printStackTrace();
}catch (IOException ioException){
ioException.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这就是我使用最简单的for循环的方式。
private void copy(final InputStream in, final OutputStream out)
throws IOException {
final byte[] b = new byte[8192];
for (int r; (r = in.read(b)) != -1;) {
out.write(b, 0, r);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
411112 次 |
| 最近记录: |