从多个文本文件读取数据

发布于 2024-12-21 14:03:08 字数 3077 浏览 3 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔女人霸气范 2024-12-28 14:03:08

在尝试打开文件名之前,您需要将目录名称添加到文件名前面。它当前正在尝试打开 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.

梦里南柯 2024-12-28 14:03:08

尝试:

File textFile = new File(folder.getAbsolutePath() + File.separator + files);

Try:

File textFile = new File(folder.getAbsolutePath() + File.separator + files);
巡山小妖精 2024-12-28 14:03:08

程序员需要学习的第一件事就是如何在遇到错误时自己弄清楚发生了什么。因此,仅仅给出答案对您没有多大帮助(尽管我看到我们已经有了答案)。

所有程序员都需要掌握的另一项核心技能是将复杂性分解为可管理的块。即使对于使用 Java 编程多年的人来说,弄清楚您发布的代码中发生了什么也是一项努力。

随着您的进一步进步,另一项非常重要的技能是了解标准库并在可能的情况下使用它。这通常可以让您免于重新发明轮子。

鉴于上述三点,我的建议是:

  • 将代码拆分为一个方法,该方法接受一个文件(或代表文件名的字符串),并计算该文件中的词频。另一种方法可以查找并循环遍历您要读取的所有文件,对每个文件调用第一个方法。
  • 分别测试您的两种方法,这将帮助您追踪错误。
  • 查看 FileFilter 接口。
  • 仔细阅读 File.name() 方法的 Javadoc。

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:

  • Split your code into one method that takes a File (or a String representing a filename), and counts word frequency in that file. And another method that finds and loops through all the files you want to read, calling the first method on each.
  • Test your two methods separately, which will help you track down the bug.
  • Look into the FileFilter interface.
  • Carefully read the Javadoc of the File.name() method.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文