I'm looking at a piece of Java code right now, and it takes a path as a String and gets its URL using
URL resource = ClassLoader.getSystemClassLoader().getResource(pathAsString);
String path = resource.getPath()
URL url = resource.toURI();
String file = resource.getFile()
Please note that I do not consider myself 100% competent to answer, but nevertheless here are some comments:
Filerepresents a file or directory accessible via file system
URL#getPathis getter on the path part of URL (
URL#getFileas per JavaDoc returns
URI is just a data structure for manipulating the generic identifier itself.
URL on the other hand is really a resource locator and offers you features to actually read the resource via registered
URLs can lead to file-system resources and you can construct URL for every file system resource by using
file:// protocol (hence
Also be aware that that
URL#getFile is unrelated to
Why do I need File object; why isn't a Resource (URL) enough?
It is enough. Only if you want to pass the resource to some component which can work only with files, you need to get
File from it. However not all resource URLs can be converted to
And is there a Resource object?
From the JRE point of view, it's just a term. Some frameworks provide you with such class (e.g. Spring's Resource).