首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java 失去文件行数

2012-09-18 
Java 得到文件行数代码如下:?public int count(String filename) throws IOException {InputStream is n

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;}
?

热点排行