aspose file tools*
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 I change it to

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

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187


Welcome to JavaRanch!

A 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: 19760

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

If you need to access a file on one of those, you should check the 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 could be both a file (located in folder message) or a folder itself (redirecting to 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:
subject: new File(URI uri)