Java 读取和处理多个文本文件
我试着创建一个程序,该程序将读取某个目录中的多个文本文件,然后生成出现在所有文本文件中的单词频率 范例 文本文件1:你好,我叫约翰,你好,我的朋友 文本文件2:天气很好 输出将显示 你好2 我的2 名字1 是3 约翰1 2 天气2 好极了 我遇到的问题是,我的程序一运行就终止了,没有显示任何输出 这是我的课Java 读取和处理多个文本文件,java,multithreading,file,Java,Multithreading,File,我试着创建一个程序,该程序将读取某个目录中的多个文本文件,然后生成出现在所有文本文件中的单词频率 范例 文本文件1:你好,我叫约翰,你好,我的朋友 文本文件2:天气很好 输出将显示 你好2 我的2 名字1 是3 约翰1 2 天气2 好极了 我遇到的问题是,我的程序一运行就终止了,没有显示任何输出 这是我的课 import java.io.File; import java.io.FileNotFoundException; import java.util.HashMap; import jav
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
public class WordCountstackquestion implements Runnable {
public String filename;
public WordCountstackquestion(String filename) {
this.filename = filename;
}
public void run() {
File file = new File("C:\\Users\\User\\Desktop\\files\\1.txt");
if (file.isDirectory()) {
Scanner in = null;
for (File files : file.listFiles()) {
int count = 0;
try {
HashMap<String, Integer> map = new HashMap<String, Integer>();
in = new Scanner(file);
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(file + " : " + count);
for (String word : map.keySet()) {
System.out.println(word + " " + map.get(word));
}
} catch (FileNotFoundException e) {
System.out.println(file + " was not found.");
}
}
}
//in.close();
}
}
更新了答案。我在问题的起因上错了。这个问题与其说是线程相关的问题,不如说是一个算法问题
Mainstackquestion
class的代码:
public class Mainstackquestion {
public static void main(String args[])
{
List<Thread> allThreads = new ArrayList<>();
if(args.length > 0) {
for (String filename : args) {
Thread t = CheckFile(filename);
allThreads.add(t); // We save this thread for later retrieval
t.start(); // We start the thread
}
}
else {
Thread t = CheckFile("C:\\Users\\User\\Desktop\\files");
allThreads.add(t);
t.start();
}
try {
for (Thread t : allThreads) {
t.join(); // We wait for the completion of ALL threads
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static Thread CheckFile(String file) {
Runnable tester = new WordCountstackquestion(file);
return new Thread(tester);
}
}
使用与示例相同的2个文件进行测试
获得的结果:
2
名字1
天气2
是3
约翰1
你好2
我的2
好极了
谢谢,但我认为我的问题不在我的Mainstackquestion类中,我认为是在我的WordCountstackquestion类中。除了你的导入(见@Henri的评论),你的
WordCountstackquestion
类对我来说似乎很好。你用我的代码测试了吗?同时修改您的导入并告诉我它为您提供了什么;-)我测试了您的代码,但在t行上出现了一个错误“Unhandled exception type InterruptedException”。join()我的错,我没有捕获到异常。我已经更新了我的答案。如果它仍然不工作,我的猜测是file.isDirectory()
返回false
或file.listFiles()
不返回任何结果,因此您的程序将退出,而根本不读取任何文件。是的,将我的代码更改为您的代码仍然只是终止程序,并且不输出任何内容。那个这就是为什么我认为这个问题与我的WordCountstackquestion
类有关;并导入java.util.*;
public class Mainstackquestion {
public static void main(String args[])
{
List<Thread> allThreads = new ArrayList<>();
if(args.length > 0) {
for (String filename : args) {
Thread t = CheckFile(filename);
allThreads.add(t); // We save this thread for later retrieval
t.start(); // We start the thread
}
}
else {
Thread t = CheckFile("C:\\Users\\User\\Desktop\\files");
allThreads.add(t);
t.start();
}
try {
for (Thread t : allThreads) {
t.join(); // We wait for the completion of ALL threads
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static Thread CheckFile(String file) {
Runnable tester = new WordCountstackquestion(file);
return new Thread(tester);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
public class WordCountstackquestion implements Runnable {
public String filename;
public WordCountstackquestion(String filename) {
this.filename = filename;
}
public void run() {
File dir = new File(filename);
if (dir.exists() && dir.isDirectory()) {
Scanner in = null;
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (File file : dir.listFiles()) {
if (file.exists() && !file.isDirectory()) {
int count = 0;
try {
in = new Scanner(file);
while (in.hasNextLine()) {
String line = in.nextLine();
String[] words = line.split(" ");
for (String w : words) {
if (map.containsKey(w)) {
map.put(w, map.get(w) + 1);
} else {
map.put(w, 1);
}
}
count++;
}
//System.out.println(file + " : " + count);
} catch (FileNotFoundException e) {
System.out.println(file + " was not found.");
} finally {
if (in != null) {
in.close();
}
}
}
}
for (String word : map.keySet()) {
System.out.println(word + " " + map.get(word));
}
}
}
}