Results 1 to 11 of 11
  1. #1
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default listing files of directory and subdirectory

    hi, i want to listing file 0f directory and subdirectory.
    i can list file and subdirectory of directory. But, i can't listing file of subdirectory.
    e.g ;

    path is /home/user/

    /home/user/file.html
    /home/user/folder
    /home/user/folder/buil.sh

    yes i could listing file.html and folder. But, i want to listing to files of subfolders "/home/user/..."

    how can i do ?

    thanks.

    Java Code:
     File dir = new File("/home/user");
    
    String[] children = dir.list();
    if (children == null) {
    // Either dir does not exist or is not a directory
    } else {
    for (int i=0; i<children.length; i++) {
    // Get filename of file or directory
    String filename = children[i];
    System.out.println(filename);
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    Quote Originally Posted by oulutas View Post
    how can i do ?
    Java Code:
    	public static void list(File path){
    		for (File file : path.listFiles()) {
    			System.out.println(file);
    			if(file.isDirectory()){
    				list(file);
    			}
    		}
    	}
    and call list(dir);

  3. #3
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    I guess I doing something wrong. I get an error message ;


    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at ftplinuxsync.Main.list(Main.java:37)
    	at ftplinuxsync.Main.main(Main.java:33)
    line 33 is = Main.list(dir);
    line 37 = for (File file : path.listFiles()) {

    Java Code:
     File dir = new File("/home/madombie");
          
           Main.list(dir);
    
        }
        public static void list(File path){
           
    		for (File file : path.listFiles()) {
    			System.out.println(file);
    			if(file.isDirectory()){
    				list(file);
    			}
    		}
            
    	}

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    What is printed out before the NPE?
    What variable is null? Add some more print outs to show the null variable.

  5. #5
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    these are all codes ;
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javadirectory;
    
    import java.io.File;
    
    /**
     *
     * @author madzombie
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            File dir = new File("/home/madombie");
          list(dir);
        }
        public static void list(File path){
    		for (File file : path.listFiles()) {
    			System.out.println(file);
    			if(file.isDirectory()){
    				list(file);
    			}
    		}
    	}
    
    }
    just print out there are ;

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
            at javadirectory.Main.list(Main.java:25)
            at javadirectory.Main.main(Main.java:22)
    after than add System.out.println(path); before for (File file : path.listFiles())

    it is print "/home/madzombie"
    but the error on 25. line. And 25. line is "for (File file : path.listFiles())"
    if i add System.out.println(path.listFiles());
    printing null
    Last edited by oulutas; 10-10-2010 at 05:25 PM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Add to your printout to show what path.listFiles() returns.

    How can the program execute the print out inside the for loop if it gets the NPE on the start of the for loop?
    Is the posted code, the code that you executed?
    Where is the: System.out.println(path) statement?

    You need to add ids to your print outs so you know which one is executing. For example:
    System.out.println("path=" + path)
    System.out.println('"fie=" + file);

  7. #7
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    i don't understand nothing.
    i add these two lines and worked.
    System.out.println("path=" + path);
    System.out.println('"fie=" + file);

    it isn't give error.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Sorry, I don't understand how adding those print out lines to your program would make it work now. They should NOT change how the program works.

  9. #9
    Maulin Patel is offline Member
    Join Date
    Oct 2010
    Location
    Vadodara
    Posts
    2
    Rep Power
    0

    Default

    try this one...

    File folder = new File("/home/user");
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
    } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
    }
    }

  10. #10
    oulutas is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I don't understand how adding those print out lines to your program would make it work now. They should NOT change how the program works.
    so yes, i don't understand too.

    Java Code:
      for (File file : path.listFiles()) {
    			System.out.println("file= " + file.getName());
                            System.out.println("path= "+path);
    			if(file.isDirectory()){
    				list(file);
    			}
    		}
    when delete these two lines, but I get the same error.

    System.out.println("file= " + file.getName());
    System.out.println("path= "+path);

    If I add running. Could not give a meaning.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    I thought the print out of the path would be the first statement in the list method, not inside of the for loop, because the value of path is NOT changed in the loop, so no need to print it every time around the loop

Similar Threads

  1. listing files at the FTP
    By oulutas in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 11:30 AM
  2. Listing directory contents
    By gandalf5166 in forum New To Java
    Replies: 8
    Last Post: 05-02-2010, 12:41 AM
  3. Replies: 8
    Last Post: 11-20-2009, 09:33 PM
  4. Listing file system from root directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 04-18-2009, 10:03 AM
  5. Deleting an directory/subdirectory/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:18 AM

Posting Permissions

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