Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes File object from java.net.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 "File object from java.net.URI " Watch "File object from java.net.URI " New topic
Author

File object from java.net.URI

Ryan Arthur Greenwood
Greenhorn

Joined: Sep 08, 2009
Posts: 4
Hi all

Is it possible to use a URI to create a File object representing a file on a REMOTE Window$ LAN share? If so does anyone have an example of the String representation of the URL such as file:/ etc. from which the following could be done: new File(new URL("file:/etc").toURI()?

Many thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

How about simply new File("\\\\remote\\share\\folder\\file.txt")? The backslashes are escaped of course to work in strings.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
And welcome to JavaRanch
Ryan Arthur Greenwood
Greenhorn

Joined: Sep 08, 2009
Posts: 4
No I'm afraid not Rob. fileObject.exists() returns false. I think that File can only represent local files, hopefully I am wrong . This brings me to the next question...

Is there any way to access remote files from an app other than via some service running on a remote machine? Don't want to install services for the sake of accessing a file.

Ryan
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Ryan Arthur Greenwood wrote:No I'm afraid not Rob. fileObject.exists() returns false. I think that File can only represent local files, hopefully I am wrong .

You are most definitely wrong
I've been able to access files on shares all the time, just using File in the way I've shown. Are you sure you have enough rights to that file? What happens if you use the same file in explorer?

If that still won't work, possibly because you need another user, JCIFS might be able to help you out.
Ryan Arthur Greenwood
Greenhorn

Joined: Sep 08, 2009
Posts: 4
Hi all

Yes thankfully I was wrong. The issue is security. The answer is to map a drive to the share with suitable authentication and then use the mapping as a URL such as:
try
{
URL url = new URL("file:/Z:/testfile.txt");
File f = new File(url.toURI());
System.out.println(f.exists());
}
catch(Exception e)
{
e.printStackTrace();
}

Thanks for the pointers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Surely you mean Don't worry about the slash - File can handle both slashes and backslashes in Windows.
Ryan Arthur Greenwood
Greenhorn

Joined: Sep 08, 2009
Posts: 4
Hey Rob

No unfortunately not. Just tried it out on 1.6 and MalformedURLException is the result of

File f = new File("Z:/testfile.txt");

Seems you need to specify protocol in the String representation of the URL.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File object from java.net.URI
 
Similar Threads
Create File object from URL. How?
reading from file
Downloading a file
Is there a way to create a new URL based on existing URL object.
getting the files from other server