繁体   English   中英

计算.txt文件Java中的行数

[英]Counting number of lines in .txt file Java

嘿,我对这个简单的程序有疑问:

package werd;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;


public class Werd {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws FileNotFoundException, IOException {


    BufferedReader in = new BufferedReader(
            new InputStreamReader(new FileInputStream("ikso.txt"), "UTF-8"));

    String line; 
    String[] tmp = null;
    int slowa = 0;
    int lines = 0;

    while ((line = in.readLine())!= null){

        lines++;
        tmp = line.split("\\s");
        System.out.println(line);
        //System.out.println(line);   

        for(String s : tmp){
            slowa++;
        }
    }
    in.close();

    System.out.println("Liczba wierszy to " +line+" a liczba slow w tekscie to " + slowa)

}


}

问题在于变量计数没有增加。 此外,Netbeans告诉我未使用变量限制。 在此页面上,我至少浏览了类似的问题。 解决行数计数的方法与我的相似。 我不明白为什么它不起作用...谢谢

从您提供的代码看来,您正在打印的是line而不是lines

计数代码看起来正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM