本帖最后由 陈中岩 于 2013-4-14 08:01 编辑
- 根据毕老师视频中自定义输入流部分
- 自己做了些改动,无意间发现问题,百思不得其解,问题标在程序中
- import java.io.File;
- import java.io.FileReader;
- import java.io.Reader;
- public class Demo {
- public static void main(String[] args) throws Exception{
- File file = new File("src/Demo.java");
- MyReader mr = new MyReader(new FileReader(file));
- String len = null;
- while ((len = mr.myreadLine()) != null) {
- System.out.println(len);
- }
- }
- }
- class MyReader {
- private Reader r;
- private int count;
- public MyReader(Reader r) {
- this.r = r;
- count = 0;
- }
- public String myreadLine() throws Exception {
- StringBuilder sb = new StringBuilder();
- ++count;
- sb.append(count+": ");//这里只添加了3个数据
- int ch = -1;
- while ((ch = r.read()) != -1) {
- if (ch == '\r') {
- continue;
- }
- else if (ch == '\n') {
- return sb.toString();
- }
- sb.append((char) ch);
- }
- if (sb.length() > 4) {//这里搞不懂为什么要大于4呢,如果写大于3的话,程序就会一直打印数字和": "
- return sb.toString();
- }
- return null;
- }
- }
复制代码 |