Results 1 to 3 of 3
- 05-21-2009, 03:12 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
(Recursively?) Getting Files in a Directory
This may be advanced, but I don't believe it is quite that complicated.
I am trying to create a program to count the total number of lines/words/characters in a directory. To do this, I need to recursively(if that is the right term) determine whether a file is a directory, and if it is, test all the files in the directory, etc. However, I cannot seem to make the program stop testing. The related code is below. Any help is greatly appreciated.
EDIT: I attached the entire code as a text file so anyone who needs to can compile it.Java Code:public class DirectoryWordLineCharCounter { private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private File[] all; private ArrayList<File> dirs = new ArrayList<File>(), files = new ArrayList<File>(); public DirectoryWordLineCharCounter(){ System.out.println("Enter the path to the directory you wish to get data for."); String dirString = ""; try { dirString = br.readLine(); } catch (IOException e) { System.err.println("An error occurred while processing your request.\nPlease try again. " + "If the problem persists, restart the program and try again.\nIf the problem still " + "is not fixed, then there may be an error in the file."); } File dir = new File(dirString); if(dir.isDirectory()){ System.out.println("File is a directory.\nPlease wait while files are processed and data is retrieved"); all = dir.listFiles(); } for(File f : all){ if(f.isDirectory()){ dirs.add(f); } else { files.add(f); } } //<<<PROBLEM POSSIBLY BETWEEN THESE LINES>>>\\ boolean hasNextLevel = true; ArrayList<File> tempFiles = new ArrayList<File>(); while(hasNextLevel){ tempFiles.clear(); hasNextLevel = false; for(File f : dirs.toArray(new File[0])){ if(f.isDirectory()){ tempFiles.add(f); hasNextLevel = true; } else{ files.add(f); } } if(hasNextLevel){ dirs.clear(); for(int i=0; i<tempFiles.size(); i++){ dirs.add(tempFiles.get(i)); } } } //<<<PROBLEM POSSIBLY BETWEEN THESE LINES>>>\\ //code for counting lines continues hereLast edited by Singing Boyo; 05-21-2009 at 03:21 AM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-21-2009, 05:03 AM #2
That's not recursion. its when a method calls itself.
for example:
and then you add threads to make it faster.Java Code:public void processDir(File x){ if(x.isDirectory()){ System.out.println(x.getAbsolutePath()); File[] f_list = x.listFiles(); for(File f : f_list){ processDir(f); } } else{ files.add(x); } }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-21-2009, 08:37 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Similar Threads
-
how to list certain files in a given directory in java
By corpusluteum in forum New To JavaReplies: 6Last Post: 09-24-2008, 12:19 AM -
load all files in a directory
By moomoo in forum New To JavaReplies: 1Last Post: 04-21-2008, 10:18 AM -
Deleting an directory/subdirectory/files
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:18 AM -
How can I get list of files in a directory
By karma in forum New To JavaReplies: 2Last Post: 12-14-2007, 11:20 PM -
how to find files in given directory
By cecily in forum New To JavaReplies: 1Last Post: 08-05-2007, 04:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks