Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2532978
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-28 10:34:06

Here is another example to show how to read a file in Java with BufferedInputStream and DataInputStreamclasses.

P.S readLine() from the type DataInputStream is deprecated. Sun officially announced this method can not convert property from bytes to characters. It’s advised to use BufferedReader

查看: java读取文本文件代码

  1. import java.io.BufferedInputStream;
  2. import java.io.DataInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7.  
  8. public class UseBufferedInputStream {
  9.  
  10.   public static void main(String[] args) {
  11.  
  12.      File file = new File("C:\\readFile.txt");
  13.      FileInputStream fis = null;
  14.      BufferedInputStream bis = null;
  15.      DataInputStream dis = null;
  16.  
  17.      try {
  18.      fis = new FileInputStream(file);
  19.  
  20.      bis = new BufferedInputStream(fis);
  21.      dis = new DataInputStream(bis);
  22.  
  23.      while (dis.available() != 0) {
  24.      System.out.println(dis.readLine());
  25.      }
  26.  
  27.      fis.close();
  28.      bis.close();
  29.      dis.close();
  30.  
  31.      } catch (FileNotFoundException e) {
  32.      e.printStackTrace();
  33.      } catch (IOException e) {
  34.      e.printStackTrace();
  35.      }
  36.      }
  37. }

阅读(918) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~