gpt4 book ai didi

java - 读取和处理多个文本文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:44 25 4
gpt4 key购买 nike

我尝试创建一个程序来读取特定目录中的多个文本文件,然后生成所有文本文件中出现的单词的频率。

例子

text file 1: hello my name is john hello my

text file 2: the weather is nice the weather is

输出会显示

hello 2

my 2

name 1

is 3

john 1

the 2

weather 2

nice 1

我遇到的问题是我的程序一运行就终止,根本没有显示任何输出。

这是我的课

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();
}
}

这是我运行它们的类

public class Mainstackquestion {
public static void main(String args[]) {
if (args.length > 0) {
for (String filename : args) {
CheckFile(filename);
}
}
else {
CheckFile("C:\\Users\\User\\Desktop\\files\\1.txt");
}
}

private static void CheckFile(String file) {
Runnable tester = new WordCountstackquestion(file);
Thread t = new Thread(tester);
t.start();
}
}

最佳答案

已更新 答案。我对问题的最初原因是错误的。该问题与其说是与线程相关的问题,不如说是算法问题。

Mainstackquestion 类的代码:

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);
}
}

WordCountstackquestion 的代码:

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));
}
}
}
}

使用您作为示例提供的相同 2 个文件进行测试。

得到的结果:

the 2

name 1

weather 2

is 3

john 1

hello 2

my 2

nice 1

关于java - 读取和处理多个文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com