博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将Java InputStream的内容写入OutputStream的简便方法
阅读量:2380 次
发布时间:2019-05-10

本文共 2745 字,大约阅读时间需要 9 分钟。

本文翻译自:

I was surprised to find today that I couldn't track down any simple way to write the contents of an InputStream to an OutputStream in Java. 今天我很惊讶地发现,我无法找到将InputStream的内容写入Java中的OutputStream任何简单方法。 Obviously, the byte buffer code isn't difficult to write, but I suspect I'm just missing something which would make my life easier (and the code clearer). 显然,字节缓冲区代码并不难写,但我怀疑我只是遗漏了一些会让我的生活更轻松(代码更清晰)的东西。

So, given an InputStream in and an OutputStream out , is there a simpler way to write the following? 因此,给定InputStream inOutputStream out ,有没有写以下简单的方法?

byte[] buffer = new byte[1024];int len = in.read(buffer);while (len != -1) {    out.write(buffer, 0, len);    len = in.read(buffer);}

#1楼

参考:


#2楼

Another possible candidate are the Guava I/O utilities: 另一个可能的候选者是Guava I / O实用程序:

I thought I'd use these since Guava is already immensely useful in my project, rather than adding yet another library for one function. 我以为我会使用这些,因为Guava在我的项目中已经非常有用,而不是为一个函数添加另一个库。


#3楼

I think it's better to use a large buffer, because most of the files are greater than 1024 bytes. 我认为最好使用大缓冲区,因为大多数文件大于1024字节。 Also it's a good practice to check the number of read bytes to be positive. 另外,最好将读取的字节数检查为正数。

byte[] buffer = new byte[4096];int n;while ((n = in.read(buffer)) > 0) {    out.write(buffer, 0, n);}out.close();

#4楼

Simple Function 简单的功能

If you only need this for writing an InputStream to a File then you can use this simple function: 如果您只需要这个来将InputStream写入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();    }}

#5楼

If you are using Java 7, (in the standard library) is the best approach: 如果您使用的是Java 7,则 (在标准库中)是最佳方法:

/* You can get Path from file also: file.toPath() */Files.copy(InputStream in, Path target)Files.copy(Path source, OutputStream out)

Edit: Of course it's just useful when you create one of InputStream or OutputStream from file. 编辑:当然,从文件创建一个InputStream或OutputStream时它非常有用。 Use file.toPath() to get path from file. 使用file.toPath()从文件中获取路径。

To write into an existing file (eg one created with File.createTempFile() ), you'll need to pass the REPLACE_EXISTING copy option (otherwise FileAlreadyExistsException is thrown): 要写入现有文件(例如,使用File.createTempFile()创建的文件),您需要传递REPLACE_EXISTING复制选项(否则抛出FileAlreadyExistsException ):

Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING)

#6楼

Use Commons Net's Util class: 使用Commons Net的Util类:

import org.apache.commons.net.io.Util;...Util.copyStream(in, out);

转载地址:http://ubexb.baihongyu.com/

你可能感兴趣的文章
不vista下安装oracle10g(r2)注意事项
查看>>
文件列表输出到文件
查看>>
Ubuntu(804) SSH远程管理服务器安装配置
查看>>
android源码
查看>>
使用Hadoop的JAVA API远程访问HDFS
查看>>
Linux下任务调度服务crond使用
查看>>
ZeroMQ的订阅发布(publish-subscribe)模式
查看>>
使用redis存储全球IP库
查看>>
Snappy Java API简介
查看>>
C/C++中正则表达式库RE2的使用
查看>>
HBase Java API(1.2.X)使用简介
查看>>
Java:实现比较接口时,应该全面的进行各种情况的比较
查看>>
python3.*下用mob_pbxproj自动化修改配置
查看>>
使用fir打包,测试跳转安装的坑
查看>>
版本号大小判断,适用规则(X.X.X.X........)
查看>>
关于Objective-C方法签名规则的说明
查看>>
libxml2.dylb 添加后找不到<libxml/tree.h> 头文件
查看>>
关于 [[self class] alloc]的理解
查看>>
Eclipse导入Web项目后代码不报错但项目报错(左上角有红叉)解决方案
查看>>
List、Set、数据结构、Collections
查看>>