java 的流的使用中,在字符缓冲输入流中,有一个每次读取一行数据的方法:readLine();

在这里使用简单的继承方法对其继续扩展,使得返回每行前面添加序号

  //需要导入的一些包
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader; //使用继承的方法,对BufferedReader类的readline()方法进行扩展 //自定义一个继承BufferedReader的类
class BufferedRLN extends BufferedReader{
int count = 0;
//构造方法,使用父类的参数
public BufferedRLN(Reader in){
super(in); //
} @Override
public String readLine() throws IOException {
//调用父类的readline()方法,返回一行数据
String content = super.readLine();
//给返回的数据设置样式(这里添加行号)
if (content == null) {
return null; //如果为空,数据已经读完
}
content = count+" "+content; //添加行号
count++;
return content;
}
} public class readline {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("C:\\Users\\lx\\Desktop\\io作业.txt"); readlineExt1(file);
} //使用自定义类 BufferedRLN
public static void readlineExt1(File file) throws IOException{ FileReader frd = new FileReader(file);
//这里的 实际参数 frd 将传到自定义里的构造方法里
BufferedRLN bfrd = new BufferedRLN(frd);
//读取数据
String string = null;
while ((string = bfrd.readLine()) != null) {
System.out.println(string);
}
//关闭资源
frd.close();
}

最新文章

  1. Linux 定时任务
  2. Sqlite 数据库出现database disk image is malformed报错的解决方法
  3. 我的STL之旅 MyStack
  4. iOS 用代码搭建UI界面实例
  5. 【SMS】移动短信网关返回信息状态代码说明【China Mobile】
  6. Spark link集合
  7. SQL 分组获取最近(大)一条记录
  8. Java学习笔记--Swing
  9. [Unity]Unity开发NGUI代码实现ScrollView(滚动视图)
  10. 关于LCD以及BMP和RGB565
  11. 手把手教小白如何用css+js实现页面中图片放大展示效果
  12. You are what you write——沈向洋
  13. 谈一谈socket与java
  14. laravel compact的用法
  15. 【BZOJ2118】墨墨的等式(最短路)
  16. BinarySearchTree二叉搜索树的实现
  17. PAT 1080 Graduate Admission[排序][难]
  18. IIS网站部署解决报错
  19. POJ3294Life Forms(广义后缀自动机)(后缀数组+二分+数状数组)
  20. jsHint-静态代码检查工具eclipse中使用

热门文章

  1. 【天道酬勤】 腾讯、百度、网易游戏、华为Offer及笔经面经(转)
  2. Redis的集群方案之Sentinel(哨兵模式)(待实践)
  3. paramiko错误信息:Paramiko error: size mismatch in put
  4. 【转】基于Linux下的TCP编程
  5. powerShell赋权限
  6. cout 堆栈,operator<< 运算符重载输出问题
  7. 字符串匹配之KMP算法(续)---还原next数组
  8. Android 布局自适应屏幕
  9. Android 智能问答机器人的实现
  10. [计算机故障]为什么我的手机SD卡一打开就是说“你的磁盘未格式化,现在需要格式化吗”?