3

same as title; how to access to UNC location in ShareFolder?

URL uri = new URL("file:\\\\x.x.1.10\\myUNC");
File file = new File(uri);

or

URL uri = new URL("file://x.x.1.10/myUNC");
File file = new File(uri);

also doesn't works.

Jeongsam Seo
  • 43
  • 1
  • 1
  • 7

3 Answers3

7

Just:

File f = new File("\\\\x.x.1.10\\myUNC");
rustyx
  • 80,671
  • 25
  • 200
  • 267
1

Using forward slashes (/) instead of backslashes with the file protocol will work to access a UNC location:

URI uri = URI.create("file:////SERVER/some/path");

Be sure, however, not to call URI.normalize() or URI.resolve() or it will break the UNC URI by removing too many slashes. The official bug: URI.normalize() ruins URI built from UNC File

More on UNC paths in java

Brad Turek
  • 2,472
  • 3
  • 30
  • 56
1

On Windows2012 R2 this worked for me

java.nio.file.Files.copy(
    Paths.get("\\\\xxxx/CRR/S20-1141_2020-03-16_ADDN1.txt"), destination, standardoptions);

The file share server is \\ (with \ escaped) but rest all /

using Java 8 240 build

Brad Turek
  • 2,472
  • 3
  • 30
  • 56
Geo V
  • 121
  • 1
  • 10
  • Please fix the formatting of your answer - for starters, if you want a line break in the post editor to be in the post (look at the preview), append two spaces to the end of the line. What is that `new`, are the parentheses really unbalanced, and how did `copy()` enter the scene? – greybeard Mar 20 '20 at 06:45