Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File object from java.net.URI

 
Ryan Arthur Greenwood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about simply new File("\\\\remote\\share\\folder\\file.txt")? The backslashes are escaped of course to work in strings.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49756
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Ryan Arthur Greenwood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you mean Don't worry about the slash - File can handle both slashes and backslashes in Windows.
 
Ryan Arthur Greenwood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic