1

What is in Java the correct way to create a file URI for Windows? I tried

new URI("file", null, file.getAbsolutePath(), null);

but this complains about a relative path used in an absolute URI. I also tried prefixing the path with "//", but this makes 'c:' into a hostname. Then I prefixed the path with "////". A subsequent uri.getPath() then has a leading "//", which still does not seem right.

Is there any clean way to go from file.getAbsolutePath() to a URI with file:// protocol and back to a Windows path usable for new File(...) on Windows?

Harald
  • 4,575
  • 5
  • 33
  • 72
  • Did u look at http://stackoverflow.com/questions/18520972/converting-java-file-url-to-file-path-platform-independent-including-u – Sheetal Mohan Sharma Dec 23 '15 at 10:55
  • Sorry guys, yes, I did searches and following proposed links when writing the question, but did not hit on the other one for some reason. I'll flag this one as duplicate myself. – Harald Dec 23 '15 at 10:57

1 Answers1

4

check File class docs. it provide toURI() method. below code seemed give output:

File file = new File("d:/myfolder/myfile.txt"); System.out.println(file.toURI());

Saurabh
  • 445
  • 6
  • 19