[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do I get the contents of a directory?

Posted on 2006-04-22
11
Medium Priority
?
242 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:MiloDC
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 252 total points
ID: 16517793
File[] list = new File(<directory>).listFilkes();

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16518829
>>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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MiloDC
ID: 16520726
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16520731
Is the server on your network (as opposed to the Internet)?
0
 

Author Comment

by:MiloDC
ID: 16520811
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
 

Author Comment

by:MiloDC
ID: 16520860
> 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
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 248 total points
ID: 16522374
>> 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
 
LVL 24

Expert Comment

by:sciuriware
ID: 16778632
:>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question