缓冲区优点:
1.更加高效
BufferedWriter
特有方法:newLine()方法。
BufferedReader
特有方法readLine()方法
自定义输出:每行加行号:
public class MyFileReader { private FileReader fr; private int lineNumber=1; MyFileReader(FileReader fr){ this.fr =fr; } public String myReadLine() throws IOException{ StringBuilder sb = new StringBuilder(); int ch =0; while((ch=fr.read())!=-1){ if(ch=='\r') continue; if(ch=='\n') return lineNumber++ +":"+sb.toString(); else sb.append((char)ch); } if(sb.length()!=0) return lineNumber++ +":"+sb.toString(); return null; } public void close() throws IOException{ fr.close(); } }