Results 1 to 11 of 11
- 10-10-2010, 02:33 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
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); }
- 10-10-2010, 02:40 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
- 10-10-2010, 03:57 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
I guess I doing something wrong. I get an error message ;
line 33 is = Main.list(dir);Java Code:Exception in thread "main" java.lang.NullPointerException at ftplinuxsync.Main.list(Main.java:37) at ftplinuxsync.Main.main(Main.java:33)
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); } } }
- 10-10-2010, 05:01 PM #4
What is printed out before the NPE?
What variable is null? Add some more print outs to show the null variable.
- 10-10-2010, 05:17 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
these are all codes ;
just print out there are ;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); } } } }
after than add System.out.println(path); before for (File file : path.listFiles())Java Code:Exception in thread "main" java.lang.NullPointerException at javadirectory.Main.list(Main.java:25) at javadirectory.Main.main(Main.java:22)
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 nullLast edited by oulutas; 10-10-2010 at 05:25 PM.
- 10-10-2010, 05:29 PM #6
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);
- 10-10-2010, 05:37 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
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.
- 10-10-2010, 05:39 PM #8
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.
- 10-10-2010, 05:41 PM #9
Member
- Join Date
- Oct 2010
- Location
- Vadodara
- Posts
- 2
- Rep Power
- 0
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-2010, 05:44 PM #10
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
so yes, i don't understand too.
when delete these two lines, but I get the same error.Java Code:for (File file : path.listFiles()) { System.out.println("file= " + file.getName()); System.out.println("path= "+path); if(file.isDirectory()){ list(file); } }
System.out.println("file= " + file.getName());
System.out.println("path= "+path);
If I add running. Could not give a meaning.
- 10-10-2010, 05:48 PM #11
Similar Threads
-
listing files at the FTP
By oulutas in forum New To JavaReplies: 6Last Post: 10-10-2010, 11:30 AM -
Listing directory contents
By gandalf5166 in forum New To JavaReplies: 8Last Post: 05-02-2010, 12:41 AM -
Recursively listing files in a directory accepted as a string??
By Zxcvtypo in forum New To JavaReplies: 8Last Post: 11-20-2009, 09:33 PM -
Listing file system from root directory
By Java Tip in forum Java TipReplies: 1Last Post: 04-18-2009, 10:03 AM -
Deleting an directory/subdirectory/files
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks