Results 1 to 2 of 2
Thread: Help with File in java
- 07-22-2007, 09:44 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
Help with File in java
I am trying to view files in a given directory on the computer inside a java program.
Basically, when given a string such as "C:\My Documents", how would i display the names of the files inside that directory?
Furthermore how would I tell if those files are directories themselves or other types of files?
Sorry I do not have any code to post but I am digging through the api docs to try and find the right classes to use and having no luck. I only need ideas for classes to use if anyone has any ideas.
Thanks
- 07-25-2007, 11:16 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 55
- Rep Power
- 0
Java Code:File dir = new File("directoryName"); 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]; } } // It is also possible to filter the list of returned files. // This example does not return any files that start with `.'. FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return !name.startsWith("."); } }; children = dir.list(filter); // The list of files can also be retrieved as File objects File[] files = dir.listFiles(); // This filter only returns directories FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { return file.isDirectory(); } }; files = dir.listFiles(fileFilter);Last edited by levent; 07-26-2007 at 06:51 PM. Reason: Code placed inside [code] tag for better readibility.
Similar Threads
-
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
Converting text file(.txt) to JPG file(.jpg) in java
By javadeveloper in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 04:22 PM -
Help with File in java
By cachi in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:58 AM -
exe File Java
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 01:50 PM -
exe file with Java
By Heather in forum New To JavaReplies: 2Last Post: 07-03-2007, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks