从多个文本文件读取数据
我是Java编程新手,我正在尝试打印名称,从文件夹中读取多个文本文件并计算每个单词文件的词频,当我读取文件夹时,所有文本文件都被打印但没有被读取,请看一下代码。
import java.io.*;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class foldersearch
{
public static void main(String[] args)
{
// Directory path here
String path = "/home/sumeet/Documents/text files";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".txt") || files.endsWith(".TXT"))
{
System.out.println(files);
TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
String currentLine="";
File textFile = new File(files); // SOME CHANGE IS REQUIRED HERE..?
try {
BufferedReader br = new BufferedReader(new FileReader(textFile));
while ((currentLine = br.readLine()) != null) {
currentLine = currentLine.toLowerCase();
StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'");
while (parser.hasMoreTokens()) {
String currentWord = parser.nextToken();
Integer frequency = frequencyMap.get(currentWord);
if (frequency == null) {
frequency = 0;
}
frequencyMap.put(currentWord, frequency + 1);
}
}
br.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(frequencyMap);
}
}
}
}
}
输出:
nokia.txt
nokia.txt (No such file or directory)
{}
MainClass.txt
MainClass.txt (No such file or directory)
{}
2b.txt
2b.txt (No such file or directory)
{}
cn exercise 2.txt
cn exercise 2.txt (No such file or directory)
{}
2c.txt
2c.txt (No such file or directory)
{}
dummy.txt
dummy.txt (No such file or directory)
{}
readme.txt
readme.txt (No such file or directory)
{}
Kb.txt
Kb.txt (No such file or directory)
{}
all.txt
all.txt (No such file or directory)
{}
1b.txt
1b.txt (No such file or directory)
{}
todo.txt
todo.txt (No such file or directory)
{}
1c.txt
1c.txt (No such file or directory)
{}
2a.txt
2a.txt (No such file or directory)
{}
USE CASE.txt
USE CASE.txt (No such file or directory)
{}
I am new to Java programming, I am trying to print names, read multiple text files from a folder and counting the word frequency of each word file, when i am reading a folder all the text files are printed but they are not read, please look at the code.
import java.io.*;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class foldersearch
{
public static void main(String[] args)
{
// Directory path here
String path = "/home/sumeet/Documents/text files";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".txt") || files.endsWith(".TXT"))
{
System.out.println(files);
TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>();
String currentLine="";
File textFile = new File(files); // SOME CHANGE IS REQUIRED HERE..?
try {
BufferedReader br = new BufferedReader(new FileReader(textFile));
while ((currentLine = br.readLine()) != null) {
currentLine = currentLine.toLowerCase();
StringTokenizer parser = new StringTokenizer(currentLine, " \t\n\r\f.,;:!?'");
while (parser.hasMoreTokens()) {
String currentWord = parser.nextToken();
Integer frequency = frequencyMap.get(currentWord);
if (frequency == null) {
frequency = 0;
}
frequencyMap.put(currentWord, frequency + 1);
}
}
br.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(frequencyMap);
}
}
}
}
}
Output:
nokia.txt
nokia.txt (No such file or directory)
{}
MainClass.txt
MainClass.txt (No such file or directory)
{}
2b.txt
2b.txt (No such file or directory)
{}
cn exercise 2.txt
cn exercise 2.txt (No such file or directory)
{}
2c.txt
2c.txt (No such file or directory)
{}
dummy.txt
dummy.txt (No such file or directory)
{}
readme.txt
readme.txt (No such file or directory)
{}
Kb.txt
Kb.txt (No such file or directory)
{}
all.txt
all.txt (No such file or directory)
{}
1b.txt
1b.txt (No such file or directory)
{}
todo.txt
todo.txt (No such file or directory)
{}
1c.txt
1c.txt (No such file or directory)
{}
2a.txt
2a.txt (No such file or directory)
{}
USE CASE.txt
USE CASE.txt (No such file or directory)
{}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在尝试打开文件名之前,您需要将目录名称添加到文件名前面。它当前正在尝试打开 nokia.txt/2b.txt/etc。从当前目录。
You need to prepend the name of the directory to the filename before you attempt to open it. It's currently trying to open nokia.txt/2b.txt/etc. from the current directory.
尝试:
Try:
程序员需要学习的第一件事就是如何在遇到错误时自己弄清楚发生了什么。因此,仅仅给出答案对您没有多大帮助(尽管我看到我们已经有了答案)。
所有程序员都需要掌握的另一项核心技能是将复杂性分解为可管理的块。即使对于使用 Java 编程多年的人来说,弄清楚您发布的代码中发生了什么也是一项努力。
随着您的进一步进步,另一项非常重要的技能是了解标准库并在可能的情况下使用它。这通常可以让您免于重新发明轮子。
鉴于上述三点,我的建议是:
One of the first things a programmer needs to learn is how to figure out for oneself what is happening when one gets an error. So we wouldn't be helping you much by just giving the answer (though I see that we already have).
Another core skill all programmers need to acquire is breaking up complexity into manageable chunks. Even for someone who's been programming in Java for years, it's an effort to figure out what's going on in the code you posted.
A further skill that's very important as you progress further is knowing the standard library and using it when you can. This can often save you from reinventing the wheel.
In light of the above 3 points, here's my advice: