我通过在测试文件夹中保留 3 个不同的文件来实现如下所示的想法
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);
}
}}
System.out.println(result);
}}
但是作为输出的结果,我得到的计算其中代码行数的文件全部都在一行中,如下所示。
{ValidateWagRewardsRedemptionOptionPPI.java=73, IWalgreensRewardsPosLogSupport.java=134, WagEnrollmentInfoLine.java=111}
请告知我希望结果采用如下所示的输出格式
WalgreensRewardsPosLogSupport.java=134,
WagEnrollmentInfoLine.java=111,
ValidateRewardsAARPManualEntryPPI.java=67
这是新行本身中的每个文件,请告知需要为此进行哪些必要的更改。
最佳答案
将 System.out.println(result);
替换为
for(String e : result.keySet())
System.out.println(e+"="+result(e)+"\n")
您得到的是 HashMap 的默认 toString。
关于java - 关于控制台中单独显示的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11231893/