Well, one problem I see is you don't initialize your fileList. It's set to null and remains this way for the duration of the method. You might try this:
public static List<File> ListFile(File filepath) { List<File>fileList = new ArrayList<File>(); File[] file = filepath.listFiles(); for (File f:file) { if(f.isFile()) fileList.add(f); if (f.isDirectory()) ListFile(f); } return fileList; } There could be other problems (e.g., the recursion could be a probem? Don't you want to add the results to the fileList getting returned when you call ListFile within the f.isDirectory() test?), but that's the most obvious to me. ~Karl --- On Mon, 7/20/09, fantasticfour <fantasticf...@126.com> wrote: > From: fantasticfour <fantasticf...@126.com> > Subject: [java ee programming] Is there something wrong with my code? > To: "java-ee-j2ee-programming-with-passion" > <java-ee-j2ee-programming-with-passion@googlegroups.com> > Date: Monday, July 20, 2009, 9:24 AM > Hi,everyone,i have trouble with > following code,which is for listing all file in a filepath > > public static ArrayList<File> ListFile(File > filepath) { > ArrayList<File> fileList = null; > File[] file = filepath.listFiles(); > for (File f:file) { > if(f.isFile()) > fileList.add(f); > if (f.isDirectory()) > ListFile(f); > } > return fileList; > } > Thanks ! > > > > > 网易YEAH.NET免费邮箱:您的终身免费邮箱 > > > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Java EE (J2EE) Programming with Passion!" group. To post to this group, send email to java-ee-j2ee-programming-with-passion@googlegroups.com To unsubscribe from this group, send email to java-ee-j2ee-programming-with-passion-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/java-ee-j2ee-programming-with-passion?hl=en?hl=en -~----------~----~----~----~------~----~------~--~---