根据LineNumberReader源代码修改的类,不能overwrite,所以就只能自己写一个类了。 原来的判断是 public int read(char cbuf[], int off, int len) throws IOException { synchronized (lock) { int n = super.read(cbuf, off, len); for (int i = off; i < off + n; i++) { int c = cbuf[i]; if (skipLF) { skipLF = false; if (c == '\n') continue; } switch (c) { \\这边的\r \n,它都算一行的!,只要把这里修改一下就可以了。但是好像不能继承override. case '\r': skipLF = true; case '\n': /* Fall through */ lineNumber++; break; } } return n; } } import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader;public class LineNumberReaderImpl extends BufferedReader { public LineNumberReaderImpl(Reader in) { super(in); } private int lineNumber; public int getLineNumber() { return this.lineNumber; } public int read(char cbuf[], int off, int len) throws IOException { synchronized (lock) { int n = super.read(cbuf, off, len); for (int i = off; i < off + n; i++) { int c = cbuf[i]; switch (c) { case '\r': lineNumber++; break; } } return n; } } private static final int maxSkipBufferSize = 8192; /** Skip buffer, null until allocated */ private char skipBuffer[] = null; public long skip(long n) throws IOException { if (n < 0) throw new IllegalArgumentException("skip() value is negative"); int nn = (int) Math.min(n, maxSkipBufferSize); synchronized (lock) { if ((skipBuffer == null) || (skipBuffer.length < nn)) skipBuffer = new char[nn]; long r = n; while (r > 0) { int nc = read(skipBuffer, 0, (int) Math.min(r, nn)); if (nc == -1) break; r -= nc; } return n - r; } } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { LineNumberReaderImpl r = null; final File f = new File("c:\\aaa.txt"); try { r = new LineNumberReaderImpl(new FileReader(f)); r.skip(f.length()); System.out.println(r.getLineNumber()); } finally { if (r != null) { r.close(); } } } }
/**
* 获取文件行数
* @param fileName - 文件路径
* @return 文件行数
*/
public static Integer getRowCount1(String fileName) {
Integer rowCount = 0;
File f = new File(fileName);
try {
BufferedReader br = new BufferedReader(new FileReader(f));
int ch = 0;
while((ch = br.read()) != -1) {
if(ch == '\n') {
rowCount++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rowCount;
}
* 获取文件行数
* @param fileName - 文件路径
* @return 文件行数
*/
public static Integer getRowCount1(String fileName) {
Integer rowCount = 0;
File f = new File(fileName);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
int ch = 0;
while((ch = br.read()) != -1) {
if(ch == '\n') {
rowCount++;
}
}
br.close();
} catch (FileNotFoundException e) {
try {
br.close();
} catch (IOException e1) {
}
} catch (IOException e) {
try {
br.close();
} catch (IOException e1) {
}
}
return rowCount;
}
原来的判断是 public int read(char cbuf[], int off, int len) throws IOException {
synchronized (lock) {
int n = super.read(cbuf, off, len); for (int i = off; i < off + n; i++) {
int c = cbuf[i];
if (skipLF) {
skipLF = false;
if (c == '\n')
continue;
}
switch (c) {
\\这边的\r \n,它都算一行的!,只要把这里修改一下就可以了。但是好像不能继承override.
case '\r':
skipLF = true;
case '\n': /* Fall through */
lineNumber++;
break;
}
} return n;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;public class LineNumberReaderImpl extends BufferedReader { public LineNumberReaderImpl(Reader in) {
super(in);
} private int lineNumber; public int getLineNumber() {
return this.lineNumber;
} public int read(char cbuf[], int off, int len) throws IOException {
synchronized (lock) {
int n = super.read(cbuf, off, len);
for (int i = off; i < off + n; i++) {
int c = cbuf[i]; switch (c) {
case '\r':
lineNumber++;
break;
}
} return n;
}
} private static final int maxSkipBufferSize = 8192;
/** Skip buffer, null until allocated */
private char skipBuffer[] = null; public long skip(long n) throws IOException {
if (n < 0)
throw new IllegalArgumentException("skip() value is negative");
int nn = (int) Math.min(n, maxSkipBufferSize);
synchronized (lock) {
if ((skipBuffer == null) || (skipBuffer.length < nn))
skipBuffer = new char[nn];
long r = n;
while (r > 0) {
int nc = read(skipBuffer, 0, (int) Math.min(r, nn));
if (nc == -1)
break;
r -= nc;
}
return n - r;
}
} /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
LineNumberReaderImpl r = null;
final File f = new File("c:\\aaa.txt");
try {
r = new LineNumberReaderImpl(new FileReader(f));
r.skip(f.length());
System.out.println(r.getLineNumber());
} finally {
if (r != null) {
r.close();
}
}
}
}