| | |
Recursive File List - Help me problem solve please
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
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.
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!
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.
Hello,
Code is working perfectly! No NPException. Using Eclipse SDK Version: 3.4.0.
Best of luck!
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
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
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
----------------------------------------------
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
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
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?
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?
•
•
•
•
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 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.
•
•
•
•
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?
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
----------------------------------------------
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
Hello again,
And again its working fine for me.
Regards,
And again its working fine for me.
java Syntax (Toggle Plain Text)
public static void main(String[] args) { for(File file : File.listRoots()){ // listRoots() method return list of drives. array of File type. search(file); } } public static void search(File f) { if ( !f.exists() ) return; String name = f.getName(); System.out.println(name); if ( f.isDirectory() ) { File[] files = f.listFiles(); for( int i = 0 ; i < files.length; i++ ) { search( files[i] ); } } }
Regards,
Puneet Kalra
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
www.PuneetK.com
Sun Certified Java Programmer
Admin of Pikk - Object Relational Mapping Framework
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
----------------------------------------------
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
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.
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
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
well to the code ive added the following line:
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!
anyways
thanks masijade for your reply
i tried something new
modified code below
Java Syntax (Toggle Plain Text)
public static void search(File f) { if ( !f.exists() ) return; String name = f.getName(); System.out.println(name); if ( f.isDirectory() ) { File[] files = f.listFiles(); if (files != null) { for( int i = 0 ; i < files.length; i++ ) { search( files[i] ); } if (f.length() <= 1) return; } } }
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.
![]() |
Similar Threads
- Solving Towers of Hanoi (C++)
- MIPS Recursive Programming, Help please! (Assembly)
Other Threads in the Java Forum
- Previous Thread: Requiring programs in java
- Next Thread: very basic sun tutorial base question
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






