wood burning stoves 2.0*
The moose likes Java in General and the fly likes Get File list from URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Get File list from URL" Watch "Get File list from URL" New topic
Author

Get File list from URL

robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
Hi

How can i get the list of file names from a url location .
I want something like this:

File f = new File(<url> ;
File[] fileNames = f.listFiles();

As it's not possible to access url with the above..

Thanks


Life is short play hard.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41107
    
  45
An URL is not a directory of a file system, so you can't use file I/O to access it. What exactly is the scenario you're trying to address?


Ping & DNS - my free Android networking tools app
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
Hi

My question is as one can access the file names in a local directory using the above code. How can i do the same for a url location?? And yes i know it's not possible to access url using the above code. Is there any way to do the same for a url location. using java.net.* or something else. I did tried to find the way but didn't find one!!

Thanks & regards
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There's no way to get a directory listing unless code on the remote machine wants to give you one. Some web servers are configured to give directory listings of certain directories; that would just be an HTML page containing the list of files, and you'd access it using the HttpURLConnection class.

Another possibility is a ftp:// URL. An ftp server will generally be happy to give you directory listings, if you issue the appropriate command. For these, your best best is a third-party FTP library. There's one in Apache's Jakarta Commons, I believe.


[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
If the URL is for the file: protocol, then you could convert it to a java.io.File, then use those methods to list the directory.

If the URL is for the http: protocol, then there is no concept of directories of files, and you fundamentally cannot do what you think you want to do. You will have to step back and look at the higher-level requirement you are trying to fulfill.

If the URL is for some other protocol, you'd better tell us what it is.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
robin singal
Ranch Hand

Joined: Jul 18, 2007
Posts: 102
sorry for delayed response..
I can render page with http access only and I did solved the problem.
I used the html page giving directory listing to get the list of the files and viz-a-viz using regular expression to render the file names.

Thanks for the replies.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Peter Chase:

If the URL is for the http: protocol, then there is no concept of directories of files, and you fundamentally cannot do what you think you want to do.


You can when the server is implementing WebDav, can't you?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get File list from URL
 
Similar Threads
Official Replies from Sun Microsystems
manipulating text file
Java: Convert a binary file to "text" and back again.
importing files
Memory problems with opening Images/Security