FileInputStream的read(byte[] b)方法,一次读取b.length个字节,减少硬盘和内存的交互,提高程序执行效率

1.路径问题

  idea中读取的默认当前路径为工程的根目录

  *注意是idea中

 

2.read(byte[] b)返回值为读到的字节数量,没有返回-1

  将字符读取到byte数组中

  byte[] bytes = new byte[4];
  int readCount = 0;
  while ((readCount = fileInputStream.read(bytes)) != -1){
  System.out.println(new String(bytes));
  }
  如果文件中的内容为abcdef
  执行的结果为:abcd
         efcd
  *不应该把byte数组全部转换为字符串,应该只转换读取的个数 System.out.println(new String(bytes, 0, readCount));执行结果:abcd
                                                                   ef
posted @ 2020-08-09 10:23  javase-->  阅读(473)  评论(0)    收藏  举报