打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java 输入流转字节数组
 /**
  * 输入流转字节流
  * */
 private byte[] InputStreamToByte(InputStream is) throws IOException {
  ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
  byte[] buffer=new byte[1024];
  int ch;
  /**
   *
   * */
  while ((ch = is.read(buffer)) != -1) {
   bytestream.write(buffer,0,ch);
  }
  byte data[] = bytestream.toByteArray();
  bytestream.close();
  return data;
 }



java 中 byte[]、File、InputStream 互相转换



1、将File、FileInputStream 转换为byte数组:

File file = new File("test.txt");

InputStream input = new FileInputStream(file);

byte[] byt = new byte[input.available()];

input.read(byt);

 

2、将byte数组转换为InputStream:

byte[] byt = new byte[1024];

InputStream input = new ByteArrayInputStream(byt);

 

3、将byte数组转换为File:

File file = new File('');

OutputStream output = new FileOutputStream(file);

BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);

bufferedOutput.write(byt);

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java中InputStream转化为byte[]数组
Java IO流详解
Java对比两个文件的差异,并且输出新文件与旧文件的差异、行数
Blob、InputStream、byte 互转
java 从网络Url中下载文件
16. 文件的保存与读取
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服