How to read Web resource files
A): Obtained using ServletContext object.
Pros: Arbitrary files, any path can be obtained
Cons: Must be in the Web environment
// get the Global object This . Getservletcontext (); // get the path to the P1.properties file String Path = Sc.getrealpath ("/web-inf/classes/p1.properties");
b): Obtained with ResourceBundle
Advantage: Non-web environment
Cons: Can only get properties file
// use resourcebunble Fetch resource file: Get the content of P1 resource file The default path is SRC, which is the classes directory for the web environment. Public void test21 () { // Fetch ResourceBundle object (information specifically used to obtain the properties file) ResourceBundle RB = Resourcebundle.getbundle ("P1"); // Fetch the contents of the file System.out.println (rb.getstring ("K"));
c): Using the class loader to obtain
Pros: Arbitrary path, Arbitrary file (file cannot be too large)
// How to get the ClassLoader /* * 1. Through the class name ServletContext.class.getClassLoader () 2. Through Object * This.getclass (). getClassLoader () 3. Class.forName () * get Class.forName ("ServletContext7"). getClassLoader () */ URL url this. getclass (). getClassLoader (). GetResource ("p1.properties"); = Url.getpath ();
Read Web resource file
read the resource files in the project, The IO stream in Java can actually be completed by using the IO stream in Java to read the resource file.
- First in In Web Engineering, create four resource files.
2 Create 1.txt in the root directory of the Web project.
2 Create 2.txt in the Webroot directory of the Web project.
2 Create 3.txt in the Web-inf directory of the Webroot directory of the Web project.
2 Create 4.txt in the SRC directory of the Web project.
- Create a The Java file is used to read the above four resource files.
Public classReaderfiletest {//write the ReadFile () method to complete the read work of the resource file. Public Static voidReadFile (String fileName)throwsexception{BufferedReader Reader=NewBufferedReader (NewFileReader (fileName)); String Line; while(line = Reader.readline ())! =NULL) {System.out.println (line); } reader.close (); } Public Static voidMain (string[] args)throwsException {//Read 1.txtString filename1 = "1.txt"; ReadFile (filename1); //Read 2.txtString filename2 = "Webroot/2.txt"; ReadFile (filename2); //Read 3.txtString Filename3 = "Webroot/web-inf/3.txt"; ReadFile (Filename3); //Read 4.txtString filename4 = "Src/4.txt"; ReadFile (FILENAME4); }}
- Run the The Java file prints the response information in the console.
If you want to use The content of the Servlet API to read the resource files in the Web project, and how to do it? The Getrealpath () method of the ServletContext object can be used to complete this work.
- Create a custom Servlet, using the Getrealpath () method of the ServletContext object to finish reading the resource file.
public class readfileservlet extends HttpServlet { public void doget (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {inputstream in = Getservletcontext (). getResourceAsStream ("/web-inf/classes/4.txt" public void doPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {doget (request, response); }}
There is also a common approach: using The class GetResource () method can also complete the task of reading a resource file.
Public classReadfileservletextendsHttpServlet { Public voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//using the ClassLoader to read Web project resource filesString filename = readfileservlet.class. GetResource ("/4.txt"). GetFile (); InputStream in=NewFileInputStream (NewFile (filename)); Ioutils.copy (in, out); } Public voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
Ioutils. Copy (in, out) the tool class used to copy files
Public classioutils{ Public voidCopy (InputStream In,outputstream out)throwsioexception{//create a specific stream objectBufferedinputstream Buffis =NewBufferedinputstream (in); Bufferedoutputstream Buffos=NewBufferedoutputstream (out); byte[] buf =New byte[1024]; intLen = 0; while(len = Buffis.read (buf))!=-1){ Buffos.write (buf,0, Len); Buffos.flush (); } in.close (); Out.close (); }}
Small talk--the way and path of reading Web resource files