黑马程序员技术交流社区
标题:
程序运行出错,求指导
[打印本页]
作者:
张振纲
时间:
2012-8-10 17:59
标题:
程序运行出错,求指导
/*
需求:模拟一个LineNumberReader
思路:创建一个类,并且创建相应的方法
通过创建一个StringBuilder来作为缓冲区
添加一个计数器来输出linenumber
*/
import java.io.*;
class MyLine
{
private Reader r;
private int lineNumber;
MyLine(Reader r)
{
this.r = r;
}
public void close()
{
try
{
r.close();
}
catch (IOException e )
{
throw new RuntimeException("关闭异常");
}
}
public int getLineNumber()
{
return lineNumber;
}
public void setLineNumber(int t)
{
lineNumber=t;
}
public String readLine()
{
lineNumber++;
int ch; //用于判断是否有字符
StringBuilder sb= null;
try
{
sb=new StringBuilder();
while ((ch=r.read())!=-1) //判断是否读到数据
{
if (ch=='\r')
continue;
if (ch=='\n')
return sb.toString();
else
sb.append((char)ch);
}
if ((sb.length())!=0)
{
return sb.toString();
}
return null;
}
catch ( IOException e )
{
throw new RuntimeException("关闭流异常");
}
}
}
class MyLineNumberReader
{
public static void main(String[] args)
{
FileReader fr = null;
try
{
fr = new FileReader("CalendarTest.java");
}
catch ( IOException e )
{
throw new RuntimeException("读取错误");
}
finally
{
try
{
fr.close();
}
catch (IOException e )
{
throw new RuntimeException("关闭异常");
}
}
MyLine my = new MyLine(fr);
my.readLine();
String str = null;
while ((str=my.readLine())!=null)
{
System.out.print(my.getLineNumber()+str);
}
my.close();
}
}
复制代码
首先我绝对不是重复发帖,刚才那个帖子的部分问题我自己解决了
所以重发
程序代码编译没有问题,可是不明白为什么运行会报错
QQ截图20120810175934.png
(5.45 KB, 下载次数: 11)
下载附件
2012-8-10 17:59 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2