?Java?,????ClassLoader
??getResource()
??????????getResource()
???????URL
??,????URL
???getPath()
??????????
?????????,??????????:
public class ResourcePathExample { public static void main(String[] args) { // ?????? String resourcePath = ResourcePathExample.class.getClassLoader().getResource("example.txt").getPath(); System.out.println("Resource Path: " + resourcePath); } }
?????,???????????example.txt
??????getResource()
?????????????????,?????????????
??:???getPath()
???????,??????????????,???????URL???????????????,????URLDecoder
????????:
import java.net.URLDecoder; public class ResourcePathExample { public static void main(String[] args) { // ?????? String resourcePath = ResourcePathExample.class.getClassLoader().getResource("example file.txt").getPath(); String decodedPath = URLDecoder.decode(resourcePath, "UTF-8"); System.out.println("Resource Path: " + decodedPath); } }
??????,??????example file.txt
,????example file.txt
???URLDecoder.decode()
?????????????