Results 1 to 2 of 2
  1. #1
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default 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

  2. #2
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    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 07:51 PM. Reason: Code placed inside [code] tag for better readibility.

Similar Threads

  1. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  2. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 05:22 PM
  3. Help with File in java
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:58 AM
  4. exe File Java
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 02:50 PM
  5. exe file with Java
    By Heather in forum New To Java
    Replies: 2
    Last Post: 07-03-2007, 05:54 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
  •