Java IO相互转换

byte[]和InputStream的相互转换

byte[]转换为InputStream

public static final InputStream bytes2InStream(byte[] buf) {
    return new ByteArrayInputStream(buf);
}

InputStream转换为byte[]

public static final byte[] inStream2bytes(InputStream inStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buff)) > 0) {
        baos.write(buff, 0, len);
    }
    byte[] bytes = baos.toByteArray();
    return bytes;
}
上一篇 国际化:Java平台下的Locale类
下一篇 LocalDateTime的增加和减少
目录
文章列表
1 CentOS下安装Git rpm包
CentOS下安装Git rpm包
2
fastjson支持配置的PropertyNamingStrategy四种策略
fastjson支持配置的PropertyNamingStrategy四种策略
3
Android自动朗读(TTS)的实现
Android自动朗读(TTS)的实现
4
Logstash排除字段及字段内容
Logstash排除字段及字段内容
5
Logstash配置过滤Spring Cloud心跳日志
Logstash配置过滤Spring Cloud心跳日志
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。