Recursive File List - Help me problem solve please

Thread Solved
Reply

Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Recursive File List - Help me problem solve please

 
0
  #1
Jan 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Recursive File List - Help me problem solve please

 
0
  #2
Jan 9th, 2009
Hello,

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

Best of luck!
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Recursive File List - Help me problem solve please

 
0
  #3
Jan 9th, 2009
File has a method that returns all the system "roots". See the API docs for File.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: Recursive File List - Help me problem solve please

 
0
  #4
Jan 9th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Recursive File List - Help me problem solve please

 
0
  #5
Jan 10th, 2009
Originally Posted by caps_lock View Post
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?

Originally Posted by caps_lock View Post
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.

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?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: Recursive File List - Help me problem solve please

 
0
  #6
Jan 10th, 2009
Hello again,

And again its working fine for me.

  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,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer


Admin of Pikk - Object Relational Mapping Framework
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Recursive File List - Help me problem solve please

 
0
  #7
Jan 10th, 2009
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: Recursive File List - Help me problem solve please

 
0
  #8
Jan 10th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Recursive File List - Help me problem solve please

 
0
  #9
Jan 11th, 2009
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 37
Reputation: caps_lock is an unknown quantity at this point 
Solved Threads: 0
caps_lock caps_lock is offline Offline
Light Poster

Re: Recursive File List - Help me problem solve please

 
0
  #10
Jan 11th, 2009
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

  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC