自己问的不清楚嘛。比如一个二进制文件,你拿什么判断它的行数。
比如下面的文件(od -x 显示):
===============================================
0000000 2749 6d61 6120 6620 6c69 2e65 490a 6127
0000020 206d 2061 6966 656c 0a2e
0000032
===============================================
你说有多少行?
比如下面的文件(od -x 显示):
===============================================
0000000 2749 6d61 6120 6620 6c69 2e65 490a 6127
0000020 206d 2061 6966 656c 0a2e
0000032
===============================================
你说有多少行?
知道UNIX下的wc命令吗?也是这样的方法实现的。
只能一个一个字符读过来,在程序中计数。
int count = 0;
File f = new File(fileName);
if (f.exists() == false || f.isDirectory() || f.canRead() == false) {
return count;
} boolean fileEmpty = true;
try {
BufferedReader reader = new BufferedReader(new FileReader(f));
int c;
boolean lastCharIs0D = false;
while ( (c = reader.read()) > -1) {
fileEmpty = false;
System.out.print( (char) c);
if (c == 0x0A) {
if (lastCharIs0D == false) {
count++;
}
lastCharIs0D = false;
}
else if (c == 0x0D) { //count add 1 when 0D or 0D0A
count++;
lastCharIs0D = true;
}
else {
lastCharIs0D = false;
}
}
reader.close();
}
catch (Exception e) {
e.printStackTrace();
return 0;
}
if (fileEmpty == false) {
count++;
} return count;
}
也许速度不行