Results 1 to 4 of 4

Thread: Directory listing

  1. #1

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275

    Post

    Does anybody happen to know how to retrieve the contents of a direcotry (like in a text field, print out all the files (their names) and subdirectories the current folder has)?

    This would have to be done using an applet.

    Any help would be greatly appreciated.


    -JR-
    "Blessed are we who can laugh at ourselves for we shall never cease to be amused"
    - Unknown

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    Here is a quick way of doing it, do run this stuff in an applet are you meaning to get a directory listing on the client or the server?? If client I believe you will need to have the applet be signed.

    Here is the code to do a directory listing, note: i just did this in an application, i didnt want to write a signed applet, but it isnt that hard.
    I also put sorted the output by name of file, display the size of the file if not a sub directory, and display last modified date as formated.

    import java.io.*;
    import java.util.*;
    import java.text.*;

    public class ListOfFiles {
    public static void main(String[] args) throws IOException {
    File dir = new File(".");
    File[] listing;
    listing = dir.listFiles();
    Arrays.sort(listing,new Comparator() {
    public int compare(Object o1, Object o2) {
    String s1 = o1.toString().toLowerCase();
    String s2 = o2.toString().toLowerCase();
    return s1.compareTo(s2);
    }
    });
    for(int i=0;i<listing.length;i++){
    SimpleDateFormat formatter = new SimpleDateFormat ("MMM-dd-yyyy ' ' hh:mm:ss");
    Date temp = new Date(listing[i].lastModified());
    String dateString = formatter.format(temp);
    if(!listing[i].isDirectory()){
    System.out.println(listing[i].getName()+"\t"+dateString+"\t"+listing[i].length());
    }
    else{
    System.out.println(listing[i].getName()+"\t"+dateString+"\t--");
    }

    }
    }
    }

  3. #3

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    Thanks, I'll get back to this when I have the time to work on the project.

    But thanks for the help! (By the way, the directory listing would have to be from the server)


    -JR-
    "Blessed are we who can laugh at ourselves for we shall never cease to be amused"
    - Unknown

  4. #4

    Thread Starter
    Hyperactive Member Jareware's Avatar
    Join Date
    Nov 2000
    Location
    Silicon Valley, CA
    Posts
    275
    So how would I do one of these signed applets?

    My java knowledge is somewhat limited, but I'll do my best to understand...


    -JR-
    "Blessed are we who can laugh at ourselves for we shall never cease to be amused"
    - Unknown

Posting Permissions

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



Click Here to Expand Forum to Full Width