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

红太狼 2021-09-26 23:32 426阅读 0赞

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

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

  1. File file = new File("test.txt");
  2. InputStream input = new FileInputStream(file);
  3. byte[] byt = new byte[input.available()];
  4. input.read(byt);

2、将byte数组转换为InputStream:

  1. byte[] byt = new byte[1024];
  2. InputStream input = new ByteArrayInputStream(byt);

3、将byte数组转换为File:

  1. File file = new File('');
  2. OutputStream output = new FileOutputStream(file);
  3. BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
  4. bufferedOutput.write(byt);

发表评论

表情:
评论列表 (有 0 条评论,426人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java类型转换工具

    不多说,直接上代码,亲们可以直接复制使用,里面包括了常用的类型转换,一般而言足够使用了! public class ObjectParser \{     publi