|
-
Apr 6th, 2001, 11:47 AM
#2
Hyperactive Member
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--");
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|