943,823 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4539
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 9th, 2009
0

Recursive File List - Help me problem solve please

Expand Post »
Hello

My recursive file scan is not doing what I want it to do.

I want it to be able to go through all roots (drives)...

With no user intervention, so without including "C:\\" or "." as a string in the source, or without a TextIO.java class.

import java.io.*;
import java.util.*;
 
public class RecursiveFileListAttemp1 {
    public static void main(String[] args) {
        File f = new File("C:\\");  
        search(f);
    }

    public static void search(File f) {  
        if ( !f.exists() ) return; 
        String name = f.getName(); 
        if ( f.isDirectory() ) {
        File[] files = f.listFiles();
        for( int i = 0 ; i < files.length; i++ ) {
        search( files[i] );
            }
        }
    }
}

At the moment I just get a null pointer exception...

I dont understand where Ive gone wrong in the code:
if its a file --> return the file name --> if its a directory --> list the files in the directory

Help please!
Last edited by caps_lock; Jan 9th, 2009 at 3:17 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
Jan 9th, 2009
0

Re: Recursive File List - Help me problem solve please

Hello,

Code is working perfectly! No NPException. Using Eclipse SDK Version: 3.4.0.

Best of luck!
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 9th, 2009
0

Re: Recursive File List - Help me problem solve please

File has a method that returns all the system "roots". See the API docs for File.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 9th, 2009
0

Re: Recursive File List - Help me problem solve please

really thats interesting punnetkay i wonder why

but it needs to work for me!

post above: thanks for that, but I dont want to list the roots I want to list the files and files in directories that are in all directories on a computer.

I think Ive got some stable code now. But I still want to know how to recursive scan the whole computer and not just a drive. At the moment I'm able to scan "C:\\" or "D:\\" and not both, does any one know how I can make that work?
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
Jan 10th, 2009
0

Re: Recursive File List - Help me problem solve please

Click to Expand / Collapse  Quote originally posted by caps_lock ...
post above: thanks for that, but I dont want to list the roots I want to list the files and files in directories that are in all directories on a computer.
Then what was this?

Click to Expand / Collapse  Quote originally posted by caps_lock ...
I want it to be able to go through all roots (drives)...

With no user intervention, so without including "C:\\" or "." as a string in the source, or without a TextIO.java class.
As that is the part I was responding to. You will need to use that.

Quote ...
I think Ive got some stable code now. But I still want to know how to recursive scan the whole computer and not just a drive. At the moment I'm able to scan "C:\\" or "D:\\" and not both, does any one know how I can make that work?
See above. You can only "recursively" scan each drive/root one at a time. But you can loop over what the above mentioned method returns.

Why does no one ever listen to what I say?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 10th, 2009
0

Re: Recursive File List - Help me problem solve please

Hello again,

And again its working fine for me.

java Syntax (Toggle Plain Text)
  1. public static void main(String[] args) {
  2. for(File file : File.listRoots()){ // listRoots() method return list of drives. array of File type.
  3. search(file);
  4. }
  5. }
  6.  
  7. public static void search(File f) {
  8. if ( !f.exists() ) return;
  9. String name = f.getName();
  10. System.out.println(name);
  11. if ( f.isDirectory() ) {
  12. File[] files = f.listFiles();
  13. for( int i = 0 ; i < files.length; i++ ) {
  14. search( files[i] );
  15. }
  16. }
  17. }

Regards,
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
Jan 10th, 2009
0

Re: Recursive File List - Help me problem solve please

The NPE (as the OP already knows from his post on www.java-forums.com (or is it org?)) is because listFiles will return null on an empty directory so he needs to add an if statement to check for that.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 10th, 2009
0

Re: Recursive File List - Help me problem solve please

can I add a equals() statement in the code above and from that print to the terminal window files that occur on more than one instance on the recursive scan?
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008
Jan 11th, 2009
0

Re: Recursive File List - Help me problem solve please

Detection of duplicate files isn't that simple; refer a similar thread. If that's not what you intend, a bit more explanation is needed.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 11th, 2009
0

Re: Recursive File List - Help me problem solve please

lol i started that thread but ignore that one for now lol

anyways

thanks masijade for your reply

i tried something new

modified code below

Java Syntax (Toggle Plain Text)
  1. public static void search(File f) {
  2. if ( !f.exists() ) return;
  3. String name = f.getName();
  4. System.out.println(name);
  5. if ( f.isDirectory() ) {
  6. File[] files = f.listFiles();
  7. if (files != null) {
  8. for( int i = 0 ; i < files.length; i++ ) {
  9. search( files[i] );
  10. } if (f.length() <= 1) return;
  11. }
  12.  
  13.  
  14. }
  15. }

well to the code ive added the following line:


if (f.length() <= 1) return;


but i dont think it has had any change in the list of results brought to the terminal window

can some one with more java knowledge tell me why please? any help or correction welcomed!
Last edited by caps_lock; Jan 11th, 2009 at 8:14 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
caps_lock is offline Offline
43 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Requiring programs in java
Next Thread in Java Forum Timeline: very basic sun tutorial base question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC