我正在尝试用java开发一个程序,该程序将计算给定文件夹中的文件数量以及每个单独文件中的代码行数。我目前的代码只能从文件夹中选取一个文件并计算该特定文件的代码行数。请帮助我了解如何从这里继续。
我当前的代码:
public class FileCountLine {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("E:/WalgreensRewardsPosLogSupport.java");
Scanner scanner = new Scanner(file);
int count = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
count++;
}
System.out.println("Lines in the file: " + count);
}
}
最佳答案
使用
String dir ="/home/directory";
File[] dirContents = dir.listFiles();
列出每个文件并在每个文件上应用您的代码。将文件名和行数存储在 map 中。
关于java - 计算文件中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11225058/