Read resource files in Java/javaweb

Source: Internet
Author: User

1, General engineering use I/O class to specify the absolute path of the file read

New FileInputStream ("src/main/resources/zsm.properties"= Ppt.getproperty ("MemAddr1");

2, the Web project can use ServletContext or ClassLoader to read

2.1, through ServletContext to read the resource file, the file path is relative to the Web project (such as jspservletfeature) root path.

2.2, through ClassLoader to read, file path is relative to the class directory (MAVEN project is generally classes)

Examples such as the following

(1) File location

  

(2) Code

        //use ServletContext to read the resource file, relative to the root path of the Web project (that is, jspservletfeature)Out.println ("\ n uses ServletContext to read the resource file, relative to the root path of the Web project (that is, jspservletfeature):"); Readfilebyservletcontext (Response,"Filereadfile1.properties"); Readfilebyservletcontext (Response,"/filereadfile1.properties"); Readfilebyservletcontext (Response,"Web-inf/classes/filereadfile2.properties"); Readfilebyservletcontext (Response,"/web-inf/classes/filereadfile2.properties"); Readfilebyservletcontext (Response,"Web-inf/classes/com/zsm/util/filereadfile3.properties"); Readfilebyservletcontext (Response,"/web-inf/classes/com/zsm/util/filereadfile3.properties"); //use ClassLoader to read resource files relative to the class directory (that is, classes)Out.println ("\ n reads the resource file using ClassLoader, relative to the class directory (that is, classes):"); Readfilebyclassloader (Response,".. /.. /filereadfile1.properties "); Readfilebyclassloader (Response,"/.. /.. /filereadfile1.properties "); Readfilebyclassloader (Response,"Filereadfile2.properties"); Readfilebyclassloader (Response,"/filereadfile2.properties"); Readfilebyclassloader (Response,"Com/zsm/util/filereadfile3.properties"); Readfilebyclassloader (Response,"/com/zsm/util/filereadfile3.properties"); //use ServletContext to read the resource file, relative to the root path of the Web project (that is, jspservletfeature)    synchronized voidReadfilebyservletcontext (httpservletresponse response, String FilePath)throwsIOException {InputStream in= This. Getservletcontext (). getResourceAsStream (FilePath); Properties prop=NewProperties ();        Prop.load (in); String FileName= Prop.getproperty ("FileName"); String name= Prop.getproperty ("name"); String Company= Prop.getproperty ("Company");        In.close (); Response.getwriter (). println (Messageformat.format ("Filepath={0}, Filename={1}, name={2}, Company={3}", FilePath, FileName, name, company)); }    //use ClassLoader to read resource files relative to the class directory (that is, classes)    synchronized voidReadfilebyclassloader (httpservletresponse response, String FilePath)throwsIOException {//gets the class loader to load the current classClassLoader loader = Filereadservlet.class. getClassLoader (); InputStream in=Loader.getresourceasstream (FilePath); Properties prop=NewProperties ();        Prop.load (in); String FileName= Prop.getproperty ("FileName"); String name= Prop.getproperty ("name"); String Company= Prop.getproperty ("Company");        In.close (); Response.getwriter (). println (Messageformat.format ("Filepath={0}, Filename={1}, name={2}, Company={3}", FilePath, FileName, name, company)); }
View Code

(3) Results

Read resource files in Java/javaweb

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.