简体   繁体   English

使用Java读取多个文件

[英]Reading multiple files using Java

I have a project where I have to print frequently used words across multiple files where each word must appear at least once in each file. 我有一个项目,我必须在多个文件打印常用单词,每个单词在每个文件中必须至少出现一次。 . now am done with printing the words but am stuck with how to check the next part of the condition that is highlighted in bold. 现在已经完成了打印单词的操作,但是仍然受困于如何检查以粗体突出显示的条件的下一部分。

condition is :: "where each word must appear at least once in each file" my output is something like this 条件是:: “每个单词在每个文件中必须至少出现一次”,我的输出是这样的

...
...
Word : 3 andrew 790
Word : 4 natasha 725
Word : 5 little 651
...
...

a snippet: 一段代码:

..... ..... ..... .....

    Scanner fileReader = null;

    Map<String, Words> map = new java.util.HashMap<String, Words>();

    System.out.println(" file read");

    // as command line arguments

    for (i = 0; i < fileNames.length; i++) {

        System.out.println("hi");

        fileReader = new Scanner(new FileInputStream(fileNames[i]));



        while (fileReader.hasNextLine()) { // while1 starts

            String line = fileReader.nextLine();

            String word = null;

..... ..... ..... .....

Guide me to proceed as iam a novice programmer Thanks in advance 指导我以iam新手程序员的身份继续前进

Maintain a Map<String,Integer> word2count for each file. 为每个文件维护一个Map<String,Integer> word2count

After the second file, compute the intersection of the key sets of the maps for file 1 and file 2. Using the intersection, create a map containing the words from the intersection and the sum of the counts from maps 1 and map 2. Let this be the map for "file 1". 在第二个文件之后,计算文件1和文件2的映射键集的交集。使用交集,创建一个映射,其中包含来自交集的单词以及来自映射1和映射2的计数总和。是“文件1”的映射。

Continue likewise with the map for "file 1" and the map for file 3, and all following files. 同样,继续“文件1”的映射和文件3的映射,以及所有后续文件。

If the intersection is empty after some file, stop: the resulting word list is empty. 如果某个文件后的交点为空,请停止:结果单词列表为空。

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

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