java 对readLine扩展添加行号样式
2024-08-25 21:20:07
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();
}
最新文章
- Linux 定时任务
- Sqlite 数据库出现database disk image is malformed报错的解决方法
- 我的STL之旅 MyStack
- iOS 用代码搭建UI界面实例
- 【SMS】移动短信网关返回信息状态代码说明【China Mobile】
- Spark link集合
- SQL 分组获取最近(大)一条记录
- Java学习笔记--Swing
- [Unity]Unity开发NGUI代码实现ScrollView(滚动视图)
- 关于LCD以及BMP和RGB565
- 手把手教小白如何用css+js实现页面中图片放大展示效果
- You are what you write——沈向洋
- 谈一谈socket与java
- laravel compact的用法
- 【BZOJ2118】墨墨的等式(最短路)
- BinarySearchTree二叉搜索树的实现
- PAT 1080 Graduate Admission[排序][难]
- IIS网站部署解决报错
- POJ3294Life Forms(广义后缀自动机)(后缀数组+二分+数状数组)
- jsHint-静态代码检查工具eclipse中使用
热门文章
- 【天道酬勤】 腾讯、百度、网易游戏、华为Offer及笔经面经(转)
- Redis的集群方案之Sentinel(哨兵模式)(待实践)
- paramiko错误信息:Paramiko error: size mismatch in put
- 【转】基于Linux下的TCP编程
- powerShell赋权限
- cout 堆栈,operator&;lt;&;lt; 运算符重载输出问题
- 字符串匹配之KMP算法(续)---还原next数组
- Android 布局自适应屏幕
- Android 智能问答机器人的实现
- [计算机故障]为什么我的手机SD卡一打开就是说“你的磁盘未格式化,现在需要格式化吗”?