• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

File object from java.net.URI

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Ryan Arthur Greenwood
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic