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