|
-
word count
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;
}
}
}
}
}
Similar Threads
-
By chintucs in forum Java
Replies: 1
Last Post: 01-18-2007, 02:24 PM
-
Replies: 2
Last Post: 02-14-2006, 04:36 AM
-
By barbarosa80503 in forum VB Classic
Replies: 2
Last Post: 10-28-2005, 03:33 PM
-
By James World in forum .NET
Replies: 0
Last Post: 08-13-2001, 04:22 PM
-
Replies: 2
Last Post: 08-12-2001, 03:59 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks