设为首页 加入收藏

TOP

2011年计算机二级考试JAVA知识点整理(20)
2014-10-31 18:15:13 来源: 作者: 【 】 浏览:69
Tags:2011年 计算机 二级 考试 JAVA 知识点 整理

  1.1.3.2.3 读写文本文件


  早些时候曾提到从文件里面读取字符的方法调用的消耗可能是重大的。这个问题在计算文本文件的行数的另一个例子中也可以找到。:


  import java.io.*;


  public class line1 {


  public static void main(String args[]) {


  if (args.length != 1) {


  System.err.println("missing filename");


  System.exit(1);


  }


  try {


  FileInputStream fis = new FileInputStream(args[0]);


  BufferedInputStream bis = new BufferedInputStream(fis);


  DataInputStream dis = new DataInputStream(bis);


  int cnt = 0;


  while (dis.readLine() != null)


  cnt++;


  dis.close();


  System.out.println(cnt);


  } catch (IOException e) {


  System.err.println(e);


  }


  }


  }这个程序使用老的DataInputStream.readLine 方法,该方法是使用用读取每个字符的 read 方法实现的。一个新方法是:


  import java.io.*;


  public class line2 {


  public static void main(String args[]) {


  if (args.length != 1) {


  System.err.println("missing filename");


  System.exit(1);


  }


  try {


  FileReader fr = new FileReader(args[0]);


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2011年计算机二级考试JAVA知识点.. 下一篇2011年计算机二级考试JAVA知识点..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: