Thread.currentThread().getContextClassLoader().getResource
时间: 2024-01-24 22:48:25 浏览: 204
JAVA中如何得到文件路径.pdf
This code snippet returns a URL object representing the resource specified by the given name. The resource is searched for in the classpath of the current thread's context class loader.
For example, if you have a file named "sample.txt" in the "resources" directory of your project, you can load it using the following code:
```
URL resourceUrl = Thread.currentThread().getContextClassLoader().getResource("resources/sample.txt");
```
This will return a URL object representing the location of the "sample.txt" file. You can then use this URL to read the contents of the file or perform other operations on it.
阅读全文