3

Why does this return false in java?

new File("\\\\10.10.1.4").exists() 

but

new File("\\\\10.10.1.4\\dir").exists() 

returns true.

If i open the first address (without 'dir') in windows explorer i see the content so i don't think it's a permission issue since windows can see it just fine

Same thing happens with File.listFiles(). The first address returns null but the second returns the actual content

Finally if all else fails, is there any other way to test if a UNC address (just the ip without directory) exists and to list its content?

Hilikus
  • 9,954
  • 14
  • 65
  • 118

2 Answers2

3

\\10.10.1.4 is an address, not a share reference, it does not point to a mounted file system. \\10.10.1.4\dir points to a share point which is a mounted file system.

I'd be like sending a letter to a street address, but leaving of the house number...it's not got a point of context from which it can be delievered. The same goes here. Java doesn't see \\10.10.14 as File, thus it doesn't exist (from the context of a File)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    How can i list the content of 10.10.1.4 then? i need to traverse the whole tree so i just don't have the 'dir', i need to see everything under 10.10.1.4. The trailing slash doesn't work either – Hilikus Aug 07 '12 at 14:01
  • Also, i don't think i agree with your analogy. Your analogy would be if i was trying to open "\\10.10.1.", which is an incomplete address. \\10.10.1.4 is a complete non-ambiguous address that resolves to a single device. if \\10.10.1.4\dir\ lists the content of 'dir' which is in 10.10.1.4, why doesn't \\10.10.1.4 list the content of the root in the device's share? it sounds like saying that C:\ is not a valid path because there is a dir in it, so i should try C:\dir – Hilikus Aug 07 '12 at 14:50
  • 1
    @Hilikus \\10.10.1.4 it's not a root device, it's not listed with File.listRoots, it's a service that provides share points. You could try looking at http://stackoverflow.com/questions/8480622/shares-under-ip – MadProgrammer Aug 07 '12 at 19:34
  • 1
    The host alone is not a valid UNC path, even though many applications treat those as some kind of directory that contains shares, for convenience. [UNC paths are only valid if they contain a host name and a share name](https://stackoverflow.com/a/28167214/897024). – kapex Dec 21 '17 at 08:08
2

File("\\10.10.1.4") doesn't exist.

File("\\10.10.1.4\") probably does.

The browser/explorer automatically adds the backslash and that's why it displays the contents.

Razvan
  • 9,925
  • 6
  • 38
  • 51