0

I am reading a book related to the OCP exam. I was studying about Path and URI and noticed something strange. Here is my code:

    URI u1 = new URI("file://C:/Brother/DrvLangChg/LangList.ini");
    Path f = Paths.get("C:/Brother/DrvLangChg/LangList.ini");
    Path p1 = Paths.get(u1);
    for(int i = 0; i < p1.getNameCount(); i++) {
        System.out.print(p1.getName(i) + " ");
    }
    System.out.println(p1.getRoot());
    System.out.println();
    for(int i = 0; i < f.getNameCount(); i++) {
        System.out.print(f.getName(i)+" ");
    }
    System.out.println(p1.getRoot());

And this is the output:

DrvLangChg LangList.ini \C\Brother\

Brother DrvLangChg LangList.ini \C\Brother\

What I noticed is that internally Java sets a type "file" when using the URI while the type is null with the String parameter (I am on Windows 10).

I am a little puzzled by this and I would like to learn more about this strange (in my opinion) behavior and what I should watch for if I am using Path.

Edit:Got it, thanks for the comments.

Phoenix
  • 15
  • 7
  • You're printing p1.getRoot() for both examples... – charles-allen Aug 18 '17 at 10:35
  • Your URI path is wrong. It should be `"file:///C:/Brother/DrvLangChg/LangList.ini"` – Flown Aug 18 '17 at 10:37
  • 1
    @ CodeConfident: Yeah I know about the root, my question was more about the return of .getNameCount. @Flown: Thanks. The // differs that it is treated as relative? – Phoenix Aug 18 '17 at 10:46
  • @Phoenix - Perhaps fixing the getRoot() call will make the difference obvious (like you said... one may be seen as relative and thus the root would be different). – charles-allen Aug 18 '17 at 10:56
  • @Phoenix Have a look at the [URI file scheme](https://en.wikipedia.org/wiki/File_URI_scheme) – Flown Aug 20 '17 at 09:33

0 Answers0