Search code examples
javafileurisystem

Java URI local file system resolve issue


I have a two URI's and I want to concat the file from the second URI like the below: ...

  URI uri1 = new URI("file:/C:/Users/TestUser/Desktop/Example_File/");
  URI uri2 = new URI("/Example_File.xlsx");

after uri1.resolve(uri2) I want to get -> file:/C:/Users/TestUser/Desktop/Example_File/Example_File.xlsx

... The above resolve uri returns file:/Exampe_File.xlsx, which is not my expected result. How to do concat these two URI's?


Solution

  • To resolve, the second URI should not have the leading /

    URI uri1 = new URI("C:/Users/TestUser/Desktop/Example_File/");
    URI uri2 = new URI("Example_File.xlsx");
    System.out.println(uri1.resolve(uri2)); 
    // C:/Users/TestUser/Desktop/Example_File/Example_File.xlsx