/**
*
*/
package io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* @author mishi
*
*/
public class MyLineBufferReaderDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
MyLineBufferedReader myDemo=null;
try {
myDemo = new MyLineBufferedReader(new FileReader("D:\\HaxLogs1.txt"));
String str=null;
while((str=myDemo.readLine())!=null)
{
System.out.println(myDemo.getNum()+":"+str);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
try {
myDemo.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
class MyLineBufferedReader extends Reader
{
private Reader in;
public MyLineBufferedReader(Reader in) {
// TODO 自动生成的构造函数存根
this.in = in;
}
private int num =1;
public int getNum()
{
return num;
}
public String readLine()throws IOException
{
StringBuilder sb = new StringBuilder();
int ch;
while((ch=in.read())!=-1)
{
if(ch=='\r')
continue;
if(ch=='\n')
{
num++;
return sb.toString();
}
sb.append((char)ch);
}
return null;
}
public void setLineNum(int num)
{
this.num = num;
}
@Override
public void close() throws IOException {
// TODO 自动生成的方法存根
in.close();
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
// TODO 自动生成的方法存根
return in.read(cbuf, off, len);
}
}
自己写的小Demo,和别人呼啸交流啦 |
|