Click to See Complete Forum and Search --> : How do you read a directory structure from within an applet?
Peter Smith
March 22nd, 1999, 12:16 AM
If I have an applet running on a web page, how can I get this applet to read
a directory (and then subsequent dirs) on the server, and then read the filenames
and then display the filenames (or whatever)??
is this possible?
thiru
March 22nd, 1999, 10:53 PM
Hello Peter,
Yes,you can do reading a directory.this can be done using
recursive calls(possible there are other solutions too!).i tried to read
dirs and files for my c:. it worked and the simple code is:
File f = new File("C:");
String[] flist;
flist = f.list();
the flist string array returns all the files and dirs of c:.next the dirs
must be searched again and this will continue till there are no more dirs
to search.hope this provides a start.ur feedback will be appreciated.
best wishes
thiru
Peter Smith
March 22nd, 1999, 11:07 PM
hmmm...
can this be done in an applet?
I have found some code to read a text file from the orginating host...but how can I
read a directory???????
try{
URL url = new URL("http://somewhere.com/test.txt");
// or URL url = new URL( getDocumentBase(), filename);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream() ) );
String s = in.readLine(); //read till you get a null line.
} catch(MalformedURLException e){
System.out.println("URLException:"+e);
} catch(IOException e){
System.out.println("IOException:"+e);
}
Zafir Anjum
March 23rd, 1999, 01:17 AM
Not without help from the server.
Scenario 1: You could set up the web server to display the directory listing as
a web page. Works only when you don't have any default page in that directory.
You could read the webpage and parse it. Not recommended.
Scenario 2: Install a CGI (or CGI like) script at the server. This script should
serve up the content of the directory. You'd again have to load the page using
HTTP and parse the result. Recommended.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.