/*
* 自定义rendLine方法
*/
package com.michael.lin;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo4 {
public static void main(String[] args) throws IOException{
File file = new File("c:\\data.txt");
FileReader fileReader = new FileReader(file);
String line = null;
while((line=myReadLine(fileReader))!=null){
System.out.println(line);
}
fileReader.close();
}
public static String myReadLine(FileReader fileReader) throws IOException{
//创建一个字符串缓冲类
StringBuilder sb = new StringBuilder(); //存储读取到的数据
int content = 0;
while((content=fileReader.read())!=-1){
if(content=='\r'){ //13=='\r '
continue;
}else if(content == '\n'){
break;
}else{
sb.append((char)content);
}
}
if(content == -1){
return null;
}
return sb.toString();
}
}