How do I get the contents of a directory?

Given a directory as a URL object, how do I build an array (URL, File, or String objects, I have no preference) of files in that directory?

I'm successfully reading from files in the directory, but I can't figure out how to simply build a file list.

I'm running an applet, and I need a single solution that works both locally and for an http:// address (like my file reading solution).


-- MiloDC
MiloDCAsked:
Who is Participating?
 
sciuriwareCommented:
File[] list = new File(<directory>).listFilkes();

;JOOP!
0
 
CEHJCommented:
>>I need a single solution that works both locally and for an http:// address (like my file reading solution).

There is not necessarily such a thing. It depends on the circumstances
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
MiloDCAuthor Commented:
> Comment from sciuriware:

> File[] list = new File(<directory>).listFilkes();

I already tried that.  Using file objects in that manner doesn't work for applets, since applets are prohibited from reading local files.


> Comment from mayankeagle

http://www.javaalmanac.com/egs/java.io/TraverseTree.html

Again, simply using methods of the File class does not work.  Applet security doesn't permit local read/write operations, and I don't think that File methods work on non-local files, anyway.  I have managed to read a file using the URL, InputStreamReader, and BufferedReader classes, but I've found no method in either of these classes for acquiring the contents of a directory.


>  Comment from CEHJ
>> 
>> I need a single solution that works both locally and for an http:// address (like my file reading solution).

> There is not necessarily such a thing. It depends on the circumstances

I'm starting to think the same thing.  What I'm thinking of doing now is writing code to determine whether a file is local or not, then send a command to the server (assuming the file isn't local) that requests a directory listing, whereupon I'll have to capture the server's response.  Sucks, but I see no other way at this time.


-- MiloDC
0
 
CEHJCommented:
Is the server on your network (as opposed to the Internet)?
0
 
MiloDCAuthor Commented:
The author of this page says that it can't be done with Java alone:

http://www.rgagnon.com/javadetails/java-0185.html


JScape offers a Java package that gets the job done for remote files:

http://www.jscape.com/articles/simple_ftp_using_java.html


I'm going to look into JScape's software.  If it works and no one provides a better answer for me, this question should be closed.


-- MiloDC
0
 
MiloDCAuthor Commented:
> Comment from CEHJ

> Is the server on your network (as opposed to the Internet)?

No, the server is on the Internet.  (My web site.)

Actually, I've discovered that there is undocumented yet widely used FTP support from Sun themselves:

http://swig.stanford.edu/pub/java/javadoc/sun/net/ftp/FtpClient.html

I tried importing the sun.net.ftp.FtpClient package, and it worked, so I'm going to give this a shot (the JScape APIs cost hundreds of dollars ).  I doubt it'll work for local files, but it's better than having to code it all from scratch myself.


-- MiloDC
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> Applet security doesn't permit local read/write operations

You can sign an applet for doing that: http://www.mindprod.com/jgloss/signedapplets.html

>> I have managed to read a file using the URL, but I've found no method in either of these classes for acquiring the contents of a directory.

That will also depend upon whether directory browsing is directly allowed on that web-site by the server or not. I'll check to see if there are any 3rd party tools for doing this.

0
 
sciuriwareCommented:
:>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.