Results 1 to 3 of 3
  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default (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.

    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 here
    EDIT: I attached the entire code as a text file so anyone who needs to can compile it.
    Attached Files Attached Files
    Last 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!

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    That's not recursion. its when a method calls itself.
    for example:
    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);
        }
      }
    and then you add threads to make it faster.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    ahh... thanks. You may have just helped me solve my problem
    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!

Similar Threads

  1. how to list certain files in a given directory in java
    By corpusluteum in forum New To Java
    Replies: 6
    Last Post: 09-24-2008, 12:19 AM
  2. load all files in a directory
    By moomoo in forum New To Java
    Replies: 1
    Last Post: 04-21-2008, 10:18 AM
  3. Deleting an directory/subdirectory/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:18 AM
  4. How can I get list of files in a directory
    By karma in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 11:20 PM
  5. how to find files in given directory
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •