| Author |
Recursive File List - Help me problem solve please
|
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
this is my beautiful code:
this is the not so beautiful problem:
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 show the error in my coding ways.
[HENRY: Formatted Code]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16815
|
|
compiling and running the class causes a null pointer exception.
A stack trace from an NPE gives an incredible amount of detail on the cause of the problem. Can you post it?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
If you can't access a folder, listFiles returns null. Now I know that there is one specific sub folder of C:\ that can (almost?) never be accessed: "System Volume Information". Even as an administrator I've never been able to access that file.
So you should check if files is null; if it is you simply cannot read that folder.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
post above: thanks so what does that mean i do? exclude that file? I dont know how to code that
any ways I think i 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?
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12325
|
|
Just study the methods provided in the java.io.File class and you will be able to figure it out.
In particular - study the Javadocs for all File methods you are using and note the conditions in which null is returned.
Bill
|
Java Resources at www.wbrogden.com
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
Just add an extra null-check after the call to listFiles. If the result is null don't do a thing; if it isn't continue as normal.
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
thanks rob prime I think it was that system volume file
anyways i got that sorted
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?
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12325
|
|
If you use equals() on file names you can spot identical names only, NOT the same as identical files.
Bill
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
You'll have to compare the contents for that, although you can use a short-circuit check - if the file sizes are different, then the files are different.
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
thanks again rob prime
and thanks william brogden
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!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
What are you searching for anyway?
The following is a general way of finding the first file that matches some criteria:
All that you need to do is implement the matches method:
Since you never told us your criteria we can't help you out with that.
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
Rob!
I looked into the match method
and I tried compiling your code but it didnt compile because..
Cannot return when a method is void and it needs to be void to allow it to be main to run outside the Java editor.
So I modified it obviously and...
Ive shifted things around so many times to get a differnt result but with the above version I get a list of half a dozen or so 0'z to the terminal window!
...What Im trying to do is return to the terminal window all those files that have an equal amount of bytes.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
I think you missed the return type of my search method - it was File, not void. But that only works for single returns, not for multiple returns like your case.
Instead of returning, just print out the file name:
Of course you can't do anything else with the matching files, but that's not your requirement. If it were you would need to use some collection to store all results in.
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
hello rob and everyone Ive had no success in getting files of the same size
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..
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
rob I dont think that other code you provided was doing what I wanted
I am really really appreciative of if though!
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
I think you'll want to keep a Map<Long,List><File>> then - a mapping of file sizes to lists of files with that file size.
At the end files contains all files, with their file sizes as the keys of the map. You can then iterate over each list:
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
|
hmm cant find a thanks or rep button but thanks for all that im working on implementing it now, just abit more looking into some methods and i should hopefully have it done, thanks again
|
 |
Martijn Verburg
author
Bartender
Joined: Jun 24, 2003
Posts: 3268
|
|
|
A thank you response is more than enough (and certainly appreciated). There's no rep or thanks system.
|
Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
|
 |
Imad Ali
Greenhorn
Joined: Jan 04, 2009
Posts: 21
|
|
|
oh yes why has Map been used asa pose to HashMap or TreeMap?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
I haven't been following the whole thread, but for the last question: this is a case of "program to the interface."
You declare the variable of type Map<Foo> and instantiate it as an instance of a class which implements Map.
private Map<Foo, Bar> fooMap;
. . .
fooMap = new HashMap<Foo, Bar>();
That means you could replace HashMap with TreeMap, or any other class which implements the Map interface, and the application will still work.
|
 |
 |
|
|
subject: Recursive File List - Help me problem solve please
|
|
|