Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:39:12+00:00 2026-05-27T14:39:12+00:00

I am new to Java programming, I am trying to print names, read multiple

  • 0

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)
{}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:39:12+00:00Added an answer on May 27, 2026 at 2:39 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Java-based web programming and am trying to learn JSF from the
I'm trying out several exercises from a Java programming book. I have the code
I'm new to C programming, I come from a Java background. I was wondering
I am new to thread programming in Java. To understand threading I'm trying to
Hi I am new to programming and trying to call a table from a
I am new to programming and Java and trying to write a program which
I'm trying to do some Socket programming in Java, and I'm using the BufferedStreamReader.read(char[])
sry about my english :) Im new to Java programming and i have a
First, i'm new at Java-programming and my native lang is not english, but still
I have been working on Eclipse recently. I am fairly new to java programming,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.