Hi all, This program takes the input as directory name from the user and from that directory it counts the words from all the text files within that directory. So far this is fine when reading the filenames from directory but when it goes to read text from files inside directory it returns file not found. I know the reason is at the time of reading the count words it again go back to its initial stage and does not go the directory specified by user. Could anyone help me please?

import java.io.*;

public class RecursiveDirectoryList {

public static void main(String[] args) {

String directoryName; // Directory name entered by the user.
File directory; // File object referring to the directory.

TextIO.put("Enter a directory name: ");
directoryName = TextIO.getln().trim();
directory = new File(directoryName);

if (directory.isDirectory() == false) {
if (directory.exists() == false)
TextIO.putln("There is no such directory!");
else
TextIO.putln("That file is not a directory.");
}
else {
listContents( directory, "" );
}

}


static void listContents(File dir, String indent) {

int flcount = 0;
String line=null;

BufferedReader in = null;
String filename;
long numWords = 0;

int index = 0;
boolean prevWhitespace = true;

String[] files; // List of names of files in the directory.
TextIO.putln(indent + "Directory \"" + dir.getName() + "\":");
indent += " "; // Increase the indentation for listing the contents.
files = dir.list();
for (int i = 0; i < files.length; i++) {
File f = new File(dir, files[i]);
if (f.isDirectory())
{
listContents(f, indent);
}
else
{

flcount++;
System.out.println("hello"+indent);
TextIO.putln(indent + files[i]);
filename = files[i];

try
{

FileReader fileReader = new FileReader(filename);
in = new BufferedReader(fileReader);

line = in.readLine();
System.out.println("the value of line d"+line);
}catch(Exception e){System.out.println(e);}

while (index < line.length())
{
char c = line.charAt(index++);
boolean currWhitespace = Character.isWhitespace(c);
if (prevWhitespace && !currWhitespace)
{
numWords++;
}
prevWhitespace = currWhitespace;
}


}
}
}

}