jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes new File(URI uri) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new File(URI uri)" Watch "new File(URI uri)" New topic

new File(URI uri)

samira khodami

Joined: Oct 12, 2008
Posts: 2
I want to define a file object from an object of type URI for accessing to a remote file.
I wrote this code:

but when I run this program,an exception of type java.lang.IllegalArgumentException be thrown.so I change it to

after this change the result was:
Base: http://markmail.org/message/2q3cywpmdpjjkm4l
G:\Your program\http:\markmail.org\message\2q3cywpmdpjjkm4l is not a directory.
but I want to access to http://markmail.org/message/2q3cywpmdpjjkm4l not G:\Your program\http:\markmail.org\message\2q3cywpmdpjjkm4l.
what exactly I should be doing here?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199


Welcome to JavaRanch!

A java.io.File object is a representation of a file on a locally-mounted filesystem. It cannot be used to represent a remote file. Please don't ask "but what if...?" because the answer is still no.

The only way to get a listing of the files on an HTTP server is to ask the HTTP server for it; you'll only get a listing if the server allows it. In other words, you can use, e.g., URLConnection to to a GET request to the server with the URL of the "directory"; the server may return a page that lists the files. That's the absolute best you can do.

The ftp protocol (or its illegitimate cousin, sftp) are better suited for what you're trying to do.

[Jess in Action][AskingGoodQuestions]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

Well, you should start by ignoring File.

File can only be used to access files on the local file system (and Windows shares and mounted (Samba / SMB / NFS) shares). Anything else, especially files you need to access using HTTP or FTP, are not supported by java.io.File.

If you need to access a file on one of those, you should check the java.net package instead. URL is especially useful - with its openConnection you get a URLConnection object which can give you an InputStream to read from.

As for determining whether or not something is a file, the answer is simple: you can't with the basic Java API. For folders on FTP it can be done (using Apache Commons Net or JvFTP), but HTTP files are even harder. Since for the client http://markmail.org/message/2q3cywpmdpjjkm4l could be both a file (located in folder message) or a folder itself (redirecting to http://markmail.org/message/2q3cywpmdpjjkm4l/index.html for instance), it will be (virtually) impossible to determine this information. And I haven't even mentioned URL rewriting!

How To Ask Questions How To Answer Questions
samira khodami

Joined: Oct 12, 2008
Posts: 2
Thanks for your help
It was helpful for me
I agree. Here's the link: http://aspose.com/file-tools
subject: new File(URI uri)
It's not a secret anymore!