Java get file path under resources

In Java reading the properties configuration file , we know how to read the properties configuration file. Here we know how to get the file path under the resoures file. Before getting the path of the properties configuration file, we used the Thread method as follows:
Insert picture description here


In fact, we can also obtain the path under the resources folder in the form of Thread, as follows
Insert picture description here

Thread.currentThread().getContextClassLoader().getResource("…").getPath()
Insert picture description here

It should be noted that the static folder cannot be empty, otherwise it may report that the null pointer is abnormal, because the empty folder is ignored during packaging, here we can view through the target package



In addition to this, we can also use many other methods, as follows:

this.getClass().getClassLoader().getResource("…").getPath()
XXX.class.getClassLoader().getResource("…").getPath()
Insert picture description here



ClassLoader.getSystemClassLoader().getResource("…").getPath()
Insert picture description here



ClassLoader.getSystemResource("…").getPath()
Insert picture description here

286 original articles published · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/newbie0107/article/details/104597954