如何打印单词的重复次数

问题描述 投票:0回答:1

我想做的是写一个单词并搜索在哪个文件中找到该单词及其包含的次数

示例:

狗这个词在:

file1.html - 2
file6.hmtl - 8
file25.html - 5

当前,我有这个文件,它仅显示文件名而不显示重复次数。

欢迎任何帮助。

public static void main(String[] args) throws FileNotFoundException {

        File dir = new File("/Users/Adan/Desktop/Files/"); // directory = target directory.

        if(dir.exists()){ // Directory exists then proceed.

            Pattern p = Pattern.compile("casa"); // keyword = keyword to search in files.
            ArrayList<String> list = new ArrayList<String>(); // list of files.

            System.out.println("La palabra " + p + " esta dentro de estos archivos:");


            for(File f : dir.listFiles()){
                if(!f.isFile()){

                    continue;
                }
                try
                {

                    FileInputStream fis = new FileInputStream(f);
                    byte[] data = new byte[fis.available()];
                    fis.read(data);
                    String text = new String(data);
                    Matcher m = p.matcher(text);
                    if(m.find()){
                        list.add(f.getName()); // add file to found-keyword list.

                    }

                    fis.close();
                } 
                catch(Exception e){

                System.out.println("\n\t Error processing file : "+f.getName());
                }

            }
            for (String listado : list) { 
                System.out.println(listado);//Lista
            }
        } // IF directory exists then only process.
        else{
            System.out.println("\n Directory doesn't exist.");
        }
    } 
}
java numbers word
1个回答
0
投票
public static void main(String[] args) throws FileNotFoundException {

    File dir = new File("D:/test2"); // directory = target directory.

    if (dir.exists()) { // Directory exists then proceed.

        Pattern p = Pattern.compile("sd4"); // keyword = keyword to search in files.
        Map<String, Long> occurences = new HashMap<>(); // list of files.

        System.out.println("La palabra " + p + " esta dentro de estos archivos:");


        for (File f : dir.listFiles()) {
            if (!f.isFile()) {

                continue;
            }
            try {

                FileInputStream fis = new FileInputStream(f);
                byte[] data = new byte[fis.available()];
                fis.read(data);
                String text = new String(data);
                Matcher m = p.matcher(text);
                while (m.find()) { //use while instead of if
                    occurences.put(f.getName(), occurences.getOrDefault(f.getName(), 0L) + 1); // add file to found-keyword list.
                }

                fis.close();
            } catch (Exception e) {

                System.out.println("\n\t Error processing file : " + f.getName());
            }

        }
        occurences.forEach((file, numberOfOccurrences) -> {
            System.out.println("In the file [" + file + "] the word occurs : " + numberOfOccurrences + " times");
        });
    } // IF directory exists then only process.
    else {
        System.out.println("\n Directory doesn't exist.");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.