现在的位置: 首页 > 综合 > 正文

89-java io流中fileinputstream的应用

2018年09月26日 ⁄ 综合 ⁄ 共 353字 ⁄ 字号 评论关闭

使用文件字节流读取文件的标准方法:

public static void main(String[] args) throws Exception {
  
  InputStream is = new FileInputStream("c:/test.txt");
  
  byte[] mybyte = new byte[20];
  
  int length = 0;//这个length是返回读取的实际的长度
  
  while(-1 != (length=is.read(mybyte, 0, 20) )){
   String str = new String(mybyte,0,length);//这里的length不能使用20,最后读取的可能没有20个字节,数据会多读
   System.out.print(str);
  }
  is.close();
  
 }

抱歉!评论已关闭.