maven project, java code loads the path of the resource file under resources

1 via the class loader loader,

1.  URL resource = TestMain.class.getResource("/18500228040.txt");

File file = new File(resource.getFile());
String absolutePath = file.getAbsolutePath();
In this way, the absolute path is obtained, and window and linux will get different results, and then these will be successful when running the program in the editor, but once it is packaged into a jar package, there will be problems 
. The response below the window

 

There will be a reaction like this under linux

2. String path = this.getClass().getResource("/18500228040.txt").getPath(); The loading in this way is the same as the above, but this one will get absolute relative to the drive letter under the window path, but note that a "/" will be added in front of it, which will cause the path to not find this resource when it is used, as shown in the 
figure:

Getting the correct results through interception is not a good solution after all, I will continue to do research, and then I will write it on the blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324736320&siteId=291194637