本帖最后由 翟宝海 于 2013-5-22 14:14 编辑
- class MyLineNumberReader
- {
- private Reader r;
- private int lineNumber;
- MyLineNumberReader(Reader r)
- {
- this.r = r;
- }
- public String myReadLine()throws IOException
- {
- lineNumber++;//这里的lineNumber没有初始化值,怎么通过编译的?
- StringBuilder sb = new StringBuilder();
- int ch = 0;
- 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;
- }
- public void setLineNumber(int lineNumber)
- {
- this.lineNumber = lineNumber;
- }
- public int getLineNumber()
- {
- return lineNumber;
- }
- public void myClose()throws IOException
- {
- r.close();
- }
- }
- class MyLineNumberReaderDemo
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr = new FileReader("copyTextByBuf.java");
- MyLineNumberReader mylnr = new MyLineNumberReader(fr);
- String line = null;
- while((line=mylnr.myReadLine())!=null)
- {
- System.out.println(mylnr.getLineNumber()+"::"+line);
- }
- mylnr.myClose();
- }
- }
复制代码 自己编写的测试代码- class Bian
- {
- public static void main(String[] args)
- {
- int x;//这里编译时,提示可能尚未初始化变量x
- x++;
- System.out.println(x);
- }
- }
复制代码 |
|