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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

;JOOP!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Mayank SAssociate Director - Product EngineeringCommented:
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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
CEHJCommented:
Is the server on your network (as opposed to the Internet)?
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
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
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.

sciuriwareCommented:
:>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.