Java Engineering reads resource file path problems in resources

Source: Internet
Author: User

Normal in the Java project to read the file under a path, you can use absolute path and relative path, absolute path nothing to say, relative path, that is, the path to the current class. There are different ways to read files in local projects and servers, as an example of a configuration file.

Read Resource files locally

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

1 New File ("Src/main/resources/properties/basecom.properties"); 2 New FileInputStream (file);

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

Server (TOMCAT) read resource file

When the project is deployed to Tomcat, the exception that is not found for the file path appears when you follow the top way. The search data knows that when Java engineering packages are deployed into Tomcat, the properties path changes to the top layer (classes):

And, at this point, the read file needs to be read by Stream , as follows:

1 this. getclass (). getResourceAsStream ("/properties/basecom.properties");

Where properties before the slash, relative to the calling class, the common top-level path.

Reference:

Resource from Src/main/resources to found after building with Maven

[Java] reads files from the resources directory in the JAR file

Java Engineering reads resource file path problems in resources

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.