读书人

Java 失去文件行数

发布时间: 2012-09-18 16:21:42 作者: rapoo

Java 得到文件行数

代码如下:

?

public int count(String filename) throws IOException {    InputStream is = new BufferedInputStream(new FileInputStream(filename));    try {        byte[] c = new byte[1024];        int count = 0;        int readChars = 0;        while ((readChars = is.read(c)) != -1) {            for (int i = 0; i < readChars; ++i) {                if (c[i] == '\n')                    ++count;            }        }        return count;    } finally {        is.close();    }}LineNumberReader  lnr = new LineNumberReader(new FileReader(new File("File1")));lnr.skip(Long.MAX_VALUE);System.out.println(lnr.getLineNumber());public int countLines(String filename) throws IOException {    LineNumberReader reader  = new LineNumberReader(new FileReader(filename));int cnt = 0;String lineRead = "";while ((lineRead = reader.readLine()) != null) {}cnt = reader.getLineNumber(); reader.close();return cnt;}
?

读书人网 >编程

热点推荐