我开发了一个 Java 程序来计算文件夹中的文件数。可能有 Java 文件或文本文件,它会计算其中的代码行数。这个想法是将文件名打印到控制台,然后是行数。这部分就完成了,如下图:
public class FileCountLine {
public static void main(String[] args) throws FileNotFoundException {
Map<String, Integer> result = new HashMap<String, Integer>();
File directory = new File("C:/Test/");
File[] files = directory.listFiles();
for (File file : files) {
if (file.isFile()) {
Scanner scanner = new Scanner(new FileReader(file));
int lineCount = 0;
try {
for (lineCount = 0; scanner.nextLine() != null; lineCount++);
} catch (NoSuchElementException e) {
result.put(file.getName(), lineCount);
}
}}
for( Map.Entry<String, Integer> entry:result.entrySet()){
System.out.println(entry.getKey()+" ==> "+entry.getValue());
}
}}
现在我已经硬编码了目录的位置,如下所示:
File directory = new File("C:/Test/");
我希望这更具交互性,并提示用户在控制台中输入位置。用户按下 Enter 后,它将按原样执行其余功能。