简体   繁体   中英

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

Maintain a Map<String,Integer> word2count for each file.

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".

Continue likewise with the map for "file 1" and the map for file 3, and all following files.

If the intersection is empty after some file, stop: the resulting word list is empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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