本帖最后由 在学野马 于 2014-10-17 08:25 编辑
class Myclass {
private Reader reader;
int lineNum = 0;
public Myclass() throws Exception {
// TODO Auto-generated constructor stub
FileReader fr=new FileReader("c:\\stud.txt");
}
public String read() throws Exception {
// StringBuffer sb = new StringBuffer();
StringBuilder sb=new StringBuilder();
int num = 0;
while ((num = reader.read()) != -1) {
if (num == '\r')
continue;
else if (num == '\n') {
lineNum++;
// System.out.println("ssssss"+sb.toString());
return addLineNum(sb.toString());
} else {
sb.append((char) num);
}
}
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
public String addLineNum(String s) {
return lineNum + ": " + s;
}
public void close() throws Exception {
reader.close();
}
}
class test6 {
public static void main(String args[]) throws Exception {
Myclass my = new Myclass();
String s = null;
while ((s = my.read()) != null) {
System.out.println(s);
}
}
}
|
|