package demo.io;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
class MyFileReader {
protected Reader r;
MyFileReader(FileReader r) {
this.r = r;
}
String readLine() throws IOException {
StringBuilder sb = new StringBuilder();
;
int ch = 0;
while ((ch = r.read()) != -1) {
if (ch == '\n')
return sb.toString();
else {
sb.append((char) ch);
}
}
if (sb.length() != 0)
return sb.toString();//仅读取一行
return null;
}
void close() throws IOException {
r.close();
}
}
class 模拟BufferedReader中readLine方法 {
public static void main(String[] args) {
MyFileReader r = null;
try {
FileReader fr2 = new FileReader("后期设备的配置文件.properties");
r = new MyFileReader(fr2);
String line = null;
while ((line = r.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (r != null)
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
模拟BufferedReader中readLine方法
最新推荐文章于 2023-12-21 21:26:01 发布