Results 1 to 18 of 18
  1. #1
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default Recursive File List - Help me problem solve please

    hello and so this is my code:

    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] );
    }
    }
    }
    }
    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!!

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    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?

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    There is a method in File that will return all the system "roots". Read the API docs for File.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default It worked for 7mins

    I tried it and it worked fine for about 7 mins, Then I got this:
    .
    .
    .
    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:
    Java 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.

  5. #5
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    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?

  6. #6
    shaggyoo7 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    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.

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    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:

    Java 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:

    Java Code:
    File[] files = f.listFiles();
    [B]if (files != null) {[/B]
      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.

  8. #8
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    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.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote 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?

  10. #10
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    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?

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    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.

  12. #12
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    thanks masijade for your reply

    i tried something new

    modified code below

    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;
    		}
    		
    
                }
            }
    well to the code ive added the following line:


    Java 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 02:12 AM.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    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.

  14. #14
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    Default

    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 04:18 PM.

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    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).

  16. #16
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    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)

    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;  
      //    }
    }
    please please help..

  17. #17
    caps_lock is offline Member
    Join Date
    Jan 2009
    Posts
    12
    Rep Power
    0

    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.

  18. #18
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    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.

Similar Threads

  1. Please solve the problem related to PDf reader
    By kavithaprabhaker in forum New To Java
    Replies: 5
    Last Post: 11-23-2011, 11:08 AM
  2. Help me to solve problem
    By mansoorhacker in forum Forum Guides
    Replies: 8
    Last Post: 01-24-2009, 07:29 PM
  3. Solve my Problem
    By kyo in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 03:22 PM
  4. Help me to solve problem
    By mansoorhacker in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 09:15 AM
  5. problem with recursive binary search program
    By imran_khan in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 04:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •