Transferred from: https://www.cnblogs.com/wt20/p/8320346.html
Projects often read some configuration files, so the GetResource method can play an important role
There are two main methods used, one is the bytecode file class class, the other is the ClassLoader class loader
There are two ways to use class classes:
1. Use "/" to get to the classpath path
2. Do not use "/" This is the relative path
ClassLoader class
Without the "/" notation, the classpath path is obtained.
Test code
public class Getresourcetest {public static void Main (string[] args) { System.out.println ( GetResourceTest.class.getResource ("/test.properties"). GetPath ()); System.out.println (GetResourceTest.class.getResource ("Test1.properties"). GetPath ()); System.out.println (GetResourceTest.class.getClassLoader (). GetResource ("Test.properties"). GetPath ()); System.out.println (GetResourceTest.class.getClassLoader (). GetResource ("/"));} }
Results:
/e:/ideajava/studytest/out/production/studytest/test.properties
/e:/ideajava/studytest/out/production/studytest/com/waston/test/test1.properties
/e:/ideajava/studytest/out/production/studytest/test.properties
Null
Project Package Structure
The above is written by https://www.cnblogs.com/wt20/p/8320346.html Bloggers
Recent projects require processing of the database's historical data, and because of the lack of familiarity with the timing tasks of the Oracle database, a timed task is written using Java
In the program need to read a CTL file, but, if given a directory, to the client run time may be due to the file location error can not read to the file, try to use the virtual path, no success, search the Internet using this method can get to the directory, try to use, success.
Path problems when reading external files in Java programs