permaculture playing cards*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new File(URI uri)" Watch "new File(URI uri)" New topic
Author

new File(URI uri)

samira khodami
Greenhorn

Joined: Oct 12, 2008
Posts: 2
Hi,
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?

thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

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
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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!


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
samira khodami
Greenhorn

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)
 
Similar Threads
File I/O prior to Java 1.5
JAVA I/O
Get the temp file(css and image) from the response
Substring capture To Write A list Of instances in A New Document
File Permissions