Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-09-2009, 08:28 PM
Member
 
Join Date: Jan 2009
Posts: 12
Rep Power: 0
caps_lock is on a distinguished road
Default Recursive File List - Help me problem solve please
hello and so this is my code:

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] );
}
}
}
}
and this is the 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!!
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-09-2009, 08:35 PM
Senior Member
 
Join Date: Nov 2008
Posts: 265
Rep Power: 1
neilcoffey is on a distinguished road
Default
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

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-09-2009, 08:48 PM
Senior Member
 
Join Date: Jun 2008
Posts: 902
Rep Power: 2
masijade is on a distinguished road
Default
There is a method in File that will return all the system "roots". Read the API docs for File.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-09-2009, 09:33 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,149
Rep Power: 2
CJSLMAN is on a distinguished road
Default It worked for 7mins
I tried it and it worked fine for about 7 mins, Then I got this:
Quote:
.
.
.
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)
Line the complained:
Code:
for( int i = 0 ; i < files.length; i++ )
Now... "C:\System Volume Information" is not a file. Not sure why it upchucked.

CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-10-2009, 02:21 AM
Member
 
Join Date: Jan 2009
Posts: 12
Rep Power: 0
caps_lock is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-10-2009, 02:29 AM
Member
 
Join Date: Jan 2009
Posts: 18
Rep Power: 0
shaggyoo7 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-10-2009, 02:32 AM
Senior Member
 
Join Date: Nov 2008
Posts: 265
Rep Power: 1
neilcoffey is on a distinguished road
Default
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:

Code:
for( int i = 0 ; i < files.length; i++ ) {
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.)

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:

Code:
File[] files = f.listFiles();
if (files != null) {
  for( int i = 0 ; i < files.length; i++ ) {
    ...
  }
}
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.
__________________
Neil Coffey

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-10-2009, 02:36 AM
Senior Member
 
Join Date: Nov 2008
Posts: 265
Rep Power: 1
neilcoffey is on a distinguished road
Default
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

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-10-2009, 06:55 AM
Senior Member
 
Join Date: Jun 2008
Posts: 902
Rep Power: 2
masijade is on a distinguished road
Default
Originally Posted by caps_lock View Post
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?
And like I said there is a method in the File class that will list the System "roots" simply loop over what that returns.

Why does no one ever listen to what I say?
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 01-11-2009, 01:02 AM
Member
 
Join Date: Jan 2009
Posts: 12
Rep Power: 0
caps_lock is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 01-11-2009, 08:07 AM
Senior Member
 
Join Date: Jun 2008
Posts: 902
Rep Power: 2
masijade is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 01-12-2009, 01:07 AM
Member
 
Join Date: Jan 2009
Posts: 12
Rep Power: 0
caps_lock is on a distinguished road
Default
thanks masijade for your reply

i tried something new

modified code below

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;
		}
		

            }
        }
well to the code ive added the following line:


Code:
 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; 01-12-2009 at 01:12 AM.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 01-12-2009, 07:56 AM
Senior Member
 
Join Date: Jun 2008
Posts: 902
Rep Power: 2
masijade is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 01-12-2009, 03:15 PM
Member
 
Join Date: Jan 2009
Posts: 12
Rep Power: 0
caps_lock is on a distinguished road
Default
wow im right lol


ok


i made these modifications to the code
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.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 01-12-2009, 03:18 PM
Senior Member
 
Join Date: Jun 2008
Posts: 902
Rep Power: 2
masijade is on a distinguished road
Default
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).
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 01-13-2009, 11:46 PM
Member
 
Join Date: Jan 2009
Posts: 12
Rep Power: 0
caps_lock is on a distinguished road
Default
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)

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;  
  //    }
}
please please help..
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 01-14-2009, 12:45 PM
Member
 
Join Date: Jan 2009
Posts: 12
Rep Power: 0
caps_lock is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 01-14-2009, 06:44 PM
Steve11235's Avatar
Senior Member
 
Join Date: Dec 2008
Posts: 798
Rep Power: 1
Steve11235 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me to solve problem mansoorhacker Forum Guides 8 01-24-2009 06:29 PM
Solve my Problem kyo New To Java 1 12-16-2008 02:22 PM
Help me to solve problem mansoorhacker New To Java 3 11-13-2008 08:15 AM
Please solve the problem related to PDf reader kavithaprabhaker New To Java 3 05-07-2008 12:21 PM
problem with recursive binary search program imran_khan New To Java 3 08-02-2007 03:08 PM


All times are GMT +2. The time now is 02:56 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org