Java 列出多个文件中单词的频率
我创建了一个程序,可以查看某个目录中的文本文件,然后继续列出该文件中的单词 例如,如果我的文本文件包含这个 你好,我叫约翰,你好,我的朋友 输出将显示 你好2 我的2 名字1 是1 约翰1 但是现在我希望我的程序搜索目录中的多个文本文件,并列出所有文本文件中出现的所有单词 这是我的程序,它将在一个文件中列出单词Java 列出多个文件中单词的频率,java,multithreading,frequency,Java,Multithreading,Frequency,我创建了一个程序,可以查看某个目录中的文本文件,然后继续列出该文件中的单词 例如,如果我的文本文件包含这个 你好,我叫约翰,你好,我的朋友 输出将显示 你好2 我的2 名字1 是1 约翰1 但是现在我希望我的程序搜索目录中的多个文本文件,并列出所有文本文件中出现的所有单词 这是我的程序,它将在一个文件中列出单词 import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import j
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
public class WordCountstackquestion implements Runnable {
private String filename;
public WordCountstackquestion(String filename) {
this.filename = filename;
}
public void run() {
int count = 0;
try {
HashMap<String, Integer> map = new HashMap<String, Integer>();
Scanner in = new Scanner(new File(filename));
while (in.hasNext()) {
String word = in.next();
if (map.containsKey(word))
map.put(word, map.get(word) + 1);
else {
map.put(word, 1);
}
count++;
}
System.out.println(filename + " : " + count);
for (String word : map.keySet()) {
System.out.println(word + " " + map.get(word));
}
} catch (FileNotFoundException e) {
System.out.println(filename + " was not found.");
}
}
}
我尝试使用一些在线资源来创建一个可以查看多个文件的方法。然而,我正在努力,似乎无法在我的程序中正确地实现它
每个文件都有一个工人类
int count;
@Override
public void run()
{
count = 0;
/* Count the words... */
...
++count;
...
}
然后用这个方法来使用它们
public static void main(String args[]) throws InterruptedException
{
WordCount[] counters = new WordCount[args.length];
for (int idx = 0; idx < args.length; ++idx) {
counters[idx] = new WordCount(args[idx]);
counters[idx].start();
}
int total = 0;
for (WordCount counter : counters) {
counter.join();
total += counter.count;
}
System.out.println("Total: " + total);
}
publicstaticvoidmain(字符串args[])抛出中断异常
{
WordCount[]计数器=新的WordCount[args.length];
对于(int idx=0;idx
我假设所有这些文件都位于同一个目录中。您可以这样做:
public void run() {
// Replace the link to your filename variable
File f = new File("link/to/folder/here");
// Check if file is a directory (always do this if you are going to use listFiles()
if (f.isDirectory()) {
// I've moved to scanner object outside the code in order to prevent mass creation of an object
Scanner in = null;
// Lists all files in a directory
// You could also use a for loop, but I prefer enchanced for loops
for (File file : f.listFiles()) {
// Everything here is your old code, utilizing a new file (now named "f" instead of "filename"
int count = 0;
try {
HashMap<String, Integer> map = new HashMap<String, Integer>();
in = new Scanner(f);
while (in.hasNext()) {
String word = in.next();
if (map.containsKey(word))
map.put(word, map.get(word) + 1);
else {
map.put(word, 1);
}
count++;
}
System.out.println(f + " : " + count);
for (String word : map.keySet()) {
System.out.println(word + " " + map.get(word));
}
} catch (FileNotFoundException e) {
System.out.println(file + " was not found.");
}
}
// Once done with the scanner, close it (I didn't see it in your code, so including it now)
in.close();
}
}
所以,问题是如何处理多个文件?您的程序当前输出什么?如果使用多线程,请不要直接访问变量。创建一个名为getCount()的方法并添加同步修饰符。我的程序当前将只输出单个文件中的单词列表,我希望能够执行多个文件。感谢您的帮助,但是,当我疲于实现您的代码时,我的程序将终止,而不输出任何内容?我所做的只是将我的WordCountstackquestion类中的run方法更改为您的,然后运行我的Mainstackquestion类。为什么什么都不输出,程序就终止了?提前谢谢你的帮助!我查看了它,在方法中调用了“f”变量而不是“file”变量。说明您不应该复制和粘贴代码,而是应该了解它的工作原理并自己实现(就像重新编码一样)。
public void run() {
// Replace the link to your filename variable
File f = new File("link/to/folder/here");
// Check if file is a directory (always do this if you are going to use listFiles()
if (f.isDirectory()) {
// I've moved to scanner object outside the code in order to prevent mass creation of an object
Scanner in = null;
// Lists all files in a directory
// You could also use a for loop, but I prefer enchanced for loops
for (File file : f.listFiles()) {
// Everything here is your old code, utilizing a new file (now named "f" instead of "filename"
int count = 0;
try {
HashMap<String, Integer> map = new HashMap<String, Integer>();
in = new Scanner(f);
while (in.hasNext()) {
String word = in.next();
if (map.containsKey(word))
map.put(word, map.get(word) + 1);
else {
map.put(word, 1);
}
count++;
}
System.out.println(f + " : " + count);
for (String word : map.keySet()) {
System.out.println(word + " " + map.get(word));
}
} catch (FileNotFoundException e) {
System.out.println(file + " was not found.");
}
}
// Once done with the scanner, close it (I didn't see it in your code, so including it now)
in.close();
}
}
for(String s : arraylist){
File f = new File(s);
}