Search code examples
javafile-read

Reading data from multiple text files


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

Solution

  • 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.