How can i get an users input in the format of /home/filedirectory and then list all the files in the path /home/filedirectory??

for the first part: have him type it, have him select a folder on his hd.
for the second, use the methods that are in the File class

No James everything has to be command-line no GUI components here is the snippet which i have used to get the path and list the files in that path/directory

Scanner scan = new Scanner(System.in);
        String path = scan.nextLine();//Files present in this path will be analysed to count frequency                
        File directory = new File(path);
        File[] listOfFiles = directory.listFiles();

Then i used an Enhanced for loop with buffered reader to read the contents of the file iteratively but when i tried entering the file path other than the Current Working Directory then its generating run-time exception(File Not Found)but still there are many .txt files.

and what other path are you trying to enter?
what exactly are you trying to do? recursively read all the files under the original path?

the problem is when i try executing the code and enter the path which is the current working directory of my application.class is running but when i give an alternative path to read files from that path i am getting FileNotFoundException eventhough i am having (.txt)files in that path and i am using String to store the path which is scanned from the user

print the File's absolute path to see how Java has completed the path, eg

String path = scan.nextLine();//Files present in this path will be analysed
File directory = new File(path);
System.out.println("Searching in " + directory.getAbsolutePath());

Here is the output James

[dinesh@bridgelinux Desktop]$ java FrequencyCounter
Enter the file path to analyse:
/home/dinesh
Searching in /home/dinesh
I could'nt read your files:java.io.FileNotFoundException: installationguide.txt (No such file or directory)
I could'nt read your files:java.io.FileNotFoundException: README.txt (No such file or directory)
Enter an value for k to show the top k words:
3
[dine=3, hi=3, hello=2]
Time Elapsed: 10MilliSeconds    

The following is the output if i try entering the current working directory as input

java FrequencyCounter
Enter the file path to analyse:
/home/dinesh/Desktop
Searching in /home/dinesh/Desktop
Enter an value for k to show the top k words:
3
[hi=4, hello=3, dine=3]
Time Elapsed: 43MilliSeconds

That's bizarre - can't find the file when it just gave you that file from its directory listing but I may know why ;)
Is this still the code?

for (File file : listOfFiles) {
   ... 
   files = file.getName();

   if (files.endsWith(".txt") etc
      br = new BufferedReader(new FileReader(files)); 

if so - you get the file name to check the extension, then use the file name to open the reader. The filename doesn't include the path, so this only works for your home dir. All you need to do is to open the stream using the actual File object as parameter, not just the file name string.

most likely the problem. without the correct path, it'll look for the file on the wrong place.
you can check this somewhat like this:
File f = new File(files); and then printing the path of f.

I think i have solved the problem now by doing the following actually i read the file based on the name of file using getName() method now i replaced that one with getAbsolutePath() method now no exception being thrown :) marking the thread as closed

That is a valid solution, but a bit tacky - convert the file to a String path just to convert it back to a file...
Much cleaner to just use the file directly

 br = new BufferedReader(new FileReader(file)); 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.