Results 1 to 18 of 18
- 01-09-2009, 08:28 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
Recursive File List - Help me problem solve please
hello and so this is my code:
and this is the problem:Java Code: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] ); } } } }
compiling and running the class causes a null pointer exception. I dont understand where Ive gone wrong. I want it to be able to go through all roots (drives) without user intervention, so without including "C:\\" or "." as a string in the source, or without a TextIO.java class. Help please!!
- 01-09-2009, 08:35 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Well, look at which line the NPE is occurring on. On that line, there's only one possible object that could be null. Which is it? How can you change your code to cope with that object being null?
Neil Coffey
Javamex - Java tutorials and performance info
- 01-09-2009, 08:48 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
There is a method in File that will return all the system "roots". Read the API docs for File.
- 01-09-2009, 09:33 PM #4
It worked for 7mins
I tried it and it worked fine for about 7 mins, Then I got this:
Line the complained:.
.
.
File: C:\System Volume Information
Exception in thread "main" java.lang.NullPointerException
at RecursiveFileListAttemp1.search(RecursiveFileListA ttemp1.java:15)
at RecursiveFileListAttemp1.search(RecursiveFileListA ttemp1.java:17)
at RecursiveFileListAttemp1.main(RecursiveFileListAtt emp1.java:7)
Now... "C:\System Volume Information" is not a file. Not sure why it upchucked.Java Code:for( int i = 0 ; i < files.length; i++ )
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-10-2009, 02:21 AM #5
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
thanks neilcoffey but that didnt help me! Im not that good at Java lol
thanks CSJL for trying it, appreciated.
I think I got some stbale code though 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?
- 01-10-2009, 02:29 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
Hey i am not so good at java but i think you should create another File object that shall be used to scan drive D and so on.
- 01-10-2009, 02:32 AM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
OK, I'll give you the answer, but in general, I'd advise trying to acquire the skill of diagnosing exceptions, because you're going to have to deal with tons of them!
The NullPointerException is telling you that on the line where it reports the exception as occurring, one of the object references "points" to a null object, yet you're trying to "do something" with that object (e.g. call a method on it). The offending line in your case is:
Now, in this case, there's only one actual variable referring to an object: "files". So "files" must be null, yet you're trying to query it for its length. (Remember, in Java, arrays are object too.)Java Code:for( int i = 0 ; i < files.length; i++ ) {
So why is "files" null? Well, it turns out that if you call list() or listFiles() on an empty directory, it will return null. In this case, it's just a quirk of that call, and you can work round it:
So how do you know whether something could return null to you? Well, it can be a bit tricky (and actually, I'd say it's bad design in this case -- the method should really just return an empty array if called on an empty directory), but a starting point is to look at the documentation of the method in question.Java Code:File[] files = f.listFiles(); [B]if (files != null) {[/B] for( int i = 0 ; i < files.length; i++ ) { ... } }Neil Coffey
Javamex - Java tutorials and performance info
- 01-10-2009, 02:36 AM #8
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Oh, and yes, as people have said, once you've got the code working you need to call File.listRoots() and call your method on each of those roots.
Neil Coffey
Javamex - Java tutorials and performance info
- 01-10-2009, 06:55 AM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 01-11-2009, 01:02 AM #10
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
thanks everyone for helping
now
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?
- 01-11-2009, 08:07 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
No. If it only has to do with the filename (and not it's contents), then simply add each name, as you get it, to a Set (i.e. HashSet). When the add method returns false, the name already existed in the Set, and so is a "duplicate" (at least from name).
If you want to search for actual duplicates, it's going to need a lot more work than this.
- 01-12-2009, 01:07 AM #12
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
thanks masijade for your reply
i tried something new
modified code below
well to the code ive added the following line:Java Code: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; } } }
but i dont think it has had any change in the list of results brought to the terminal windowJava Code:if (f.length() <= 1) return;
can some one with more java knowledge tell me why please? any help or correction welcomed!Last edited by caps_lock; 01-12-2009 at 01:12 AM.
- 01-12-2009, 07:56 AM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
No, you're right. It doesn't have any effect, because whether that if is true, or false, the method will return. Either from your entered return, or simply because it has reached the end of the method.
- 01-12-2009, 03:15 PM #14
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
wow im right lol
ok
i made these modifications to the code
Java Code:public static void search(File f) { //if ( !f.exists() ); this line has been commented out String name = f.getName(); if (f.length() <=1) return; long size = f.length(); System.out.println(name + size); if ( f.isDirectory() ) { File[] files = f.listFiles(); if (files != null) { for( int i = 0 ; i < files.length; i++ ) { search( files[i] ); } } } }
but now I get no results to the terminal window??Last edited by caps_lock; 01-12-2009 at 03:18 PM.
- 01-12-2009, 03:18 PM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, what will f.length() return if the "file" in question is a directory? (Which the first one always will be considering your starting out with system roots).
Seems to me, it's because you never actually start recursing (because of the above statement/question).
- 01-13-2009, 11:46 PM #16
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
hello everyone i still need help on my java code
Please may someone help me to get files of the same size listed on screen or put into an array (both if possible).
Heres what ive tried so far, (also take a look at the commented out code)
please please help..Java Code:import java.io.*; import java.util.*; public class scanner { public static void main(String[] args) { for(File file : File.listRoots()){ search(file); // duplicate(file); } } public static void search(File f) { //if (!f.exists()) return; // String name = f.getName(); // System.out.println(name); // if (duplicate(f)) System.out.println(f); if ( f.isDirectory() ) { File[] files = f.listFiles(); //if (files.length == files.length) return; { for( int i = 0 ; i < files.length; i++ ) { if (files[i].length() == files[i].length()) { search( files[i] ); //if (files[i].length() == files[i].length()) return; System.out.println(files[i]); } } } } //public static void duplicate(File file) //{ //if (file.length() == file.length()) return; // } }
- 01-14-2009, 12:45 PM #17
Member
- Join Date
- Jan 2009
- Posts
- 12
- Rep Power
- 0
so just to add more detail because theyre may be confusion
Im basically trying to get files with equal file size first, then im trying to put those in an array, then the files in the array need to be compared possibily by length (bytes) (not file names but files contents), the comparison need to be made maybe by binary contents or by generating an MD checksum which ever ones easier.
- 01-14-2009, 06:44 PM #18
So, you want to sort all your files by size. If two files are the same size, you want to compare their contents. If they are identical, you want to list them.
Create a TreeMap<Long, File> and put the file bytes and file into it. This will sort all your files by the number of bytes. You will have to wrap the bytes with Long.
for (File file: myTreeMap.values()) {...} This will loop through all the files in ascending bytes order. You will need a File previousFile variable defined outside the loop and updated inside so you can make comparisons. If the bytes match, then you can compare the contents by opening input streams on the files and doing a byte by byte comparison.
Similar Threads
-
Please solve the problem related to PDf reader
By kavithaprabhaker in forum New To JavaReplies: 5Last Post: 11-23-2011, 10:08 AM -
Help me to solve problem
By mansoorhacker in forum Forum GuidesReplies: 8Last Post: 01-24-2009, 06:29 PM -
Solve my Problem
By kyo in forum New To JavaReplies: 1Last Post: 12-16-2008, 02:22 PM -
Help me to solve problem
By mansoorhacker in forum New To JavaReplies: 3Last Post: 11-13-2008, 08:15 AM -
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08-02-2007, 03:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks