当前位置:网站首页>用Reader模拟BufferedReader的readLine()功能
用Reader模拟BufferedReader的readLine()功能
2022-07-20 11:10:00 【菜鸟养成计划111】
import java.io.FileReader;
public class Moni1 {
public static void main(String[] args) throws Exception {
MyReader reader=new MyReader(new FileReader("b.txt"));
String s=null;
while((s=reader.readerline())!=null) {
System.out.println(s);
}
reader.close();
}
}
//Reader类
class MyReader{
private FileReader r;
//返回值为字符串的有参方法
public MyReader(FileReader r) {
// TODO Auto-generated constructor stub
this.r=r;
}
public String readerline( ) throws Exception {
StringBuffer s=new StringBuffer();//调用append方法
int n=0;
while((n=r.read())!=-1) {
if(n=='\r') {
//如果是空格则跳过
continue;
}else if(n=='\n'){
//如果是换行则返回本行已读字符串
return s.toString();
}
s.append((char)n);
}
if(s.length()>0) {
return s.toString();
}
return null;
}
public void close() throws Exception {
r.close();
}
}
边栏推荐
- 去掉<li>前面空白距离
- 数仓之数据质量建设
- xts performance auto fix script
- Mobile end adaptation scheme and how to fix the width of the window
- Golang-- operation date
- 一文详解图像中的无监督学习
- 【Flutter -- 基础组件】文本组件 Text & TextStyle & TextSpan
- Babang MS Marco! Transformer based hybrid list aware sorting model
- 历史上的今天:Kotlin 语言首次被公开;IMAP 协议之父出生;CT 成像实现新突破...
- Nacos服务发现数据模型
猜你喜欢
随机推荐
(note) feature fusion method in neural network (add/connect)
DDR 6 memory has been put into research and development
【Go开源宝藏】基于 Golang 语法的性能调优技巧(字符串拼接)
MySQL 啥时候用表锁,啥时候用行锁?
京准电钟北斗时钟服务器,GPS网络时间服务器,NTP卫星授时系统
使用Fiddler对app进行抓包
MoCo V3:视觉自监督迎来Transformer
(note) CIE chromaticity diagram
ORM框架介绍——什么是ORM框架?
基于MCU通用GUI大盘点
编程必备,程序员应该都知道的7款文本编辑器
着力提升妇儿获得感、幸福感、安全感!未来十年广州妇女儿童事业这样发力
如何通过函数返回数组首地址
视频分析StreamEye Studio
用 emoji 学安全上网小常识?看 Google 新玩法
去掉<li>前面空白距离
Global location number Gln application introduction
JVM 基础
What can testers do when there is an online bug?
哲学的起源和发展-黑格尔-外在历史形态