On the problem of reading resource file path in resources by java project

When reading a file under a certain path in a java project, you can use absolute path and relative path. There is nothing to say about absolute path. Relative path is the path relative to the current class. There are different ways to read files in the local project and the server. The following figure shows the configuration file as an example.

Read resource file locally

The Java class needs to read the configuration file in properties, which can be read in the form of file:

When running in eclipse (not deployed to the server), you can read the file.

The server (Tomcat) reads the resource file

When the project is deployed to tomcat, an exception will appear that the file path cannot be found according to the above method. According to the search data, when the Java project is packaged and deployed in tomcat, the path of properties changes to the top level (under classes):

In addition, at this time, the file needs to be read by stream, as follows:

The slash before properties is a common top-level path relative to the calling class.

The above discussion on reading the resource file path in resources in Java project is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>