自己写的工具类,希望适用/**
* 按行读取文件
* @param file
* @param fromLineNum
* @param charset
* @return
*/
public static List readLines(File file,int fromLineNum,Charset charset) {
List lines=new ArrayList();
int prechar=-1;
int lineNum=0;
InputStream is=null;
try {
is=new FileInputStream(file);
int data=0;
List bList=new ArrayList();
while((data=is.read())!=-1) {
byte x=(byte) (data>127?data-256:data);
if(data=='\n') {
lineNum+=1;
if(prechar=='\r') {
bList.remove(bList.size()-1);
}
byte bs[]=new byte[bList.size()];
for(int i=0;i
bs[i]=bList.get(i);
}
if(lineNum>=fromLineNum) {
String line=new String(bs,0,bs.length,charset);
lines.add(line);
}
bList.clear();
}else {
bList.add(x);
}
prechar=data;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return lines;
}