ServletContext中getRealPath()讀取文件及其他三種讀取文件的方式及其區別
前提條件:
1,新建webproiect為20140806-servlet-servletresponse-servletrequest
2,第一個配置文件是在工程的src根目錄下建立filesrc.properties。並且里面的鍵值對為:src=filesrc
3,第二個配置文件在src目錄的包com.itheima.servletcontext.fourreadresource中建立filecom.properties,並且里面的鍵值對為:com=filecom
4,第三個配置文件在WEB-INF的根目錄下建立filewebinf.properties,里面的鍵值對為:webinf=filewebinf
建立好之后如圖:
下面是具體的代碼實現:
- package com.itheima.servletcontext.fourreadresource;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.util.Properties;
- import java.util.ResourceBundle;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ReadPrepertiesServletContextDemo7 extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // test00();
- // test01();
- // test02();
- // test10();
- // test11();
- // test21();
- // test22();
- // test23();
- test30();
- }
- <span style="white-space:pre"> </span>//按照倒序,第四種在上面,servletcontext的getRealPath方法在最后
- //=============================================================================================
- //通過類加載器 ,默認一上來就已經定位到classes目錄
- //通過URL來獲取文件的路徑
- private void test30() throws FileNotFoundException, IOException {
- ClassLoader cl=this.getClass().getClassLoader();
- URL url=cl.getResource("filesrc.properties");
- String path=url.getPath();
- Properties prop=new Properties();
- prop.load(new FileInputStream(path));
- System.out.println(prop.getProperty("src"));//
- }
- //=============================================================================================
- //通過類加載器 ,默認一上來就已經定位到classes目錄
- private void test21() throws FileNotFoundException, IOException {
- ClassLoader cl=this.getClass().getClassLoader();
- InputStream ins=cl.getResourceAsStream("filesrc.properties");
- Properties prop=new Properties();
- prop.load(ins);
- System.out.println(prop.getProperty("src"));
- }
- //通過類加載器 ,獲取classes目錄下的包里的配置文件
- private void test22() throws FileNotFoundException, IOException {
- ClassLoader cl=this.getClass().getClassLoader();
- //此處需要把包名的.換成/
- InputStream ins=cl.getResourceAsStream("com/itheima/servletcontext/fourreadresource/filecom.properties");
- Properties prop=new Properties();
- prop.load(ins);
- System.out.println(prop.getProperty("com"));
- }
- //通過類加載器 ,獲取classes上上一級目錄下的配置文件
- private void test23() throws FileNotFoundException, IOException {
- ClassLoader cl=this.getClass().getClassLoader();
- //此處需要把包名的.換成/
- InputStream ins=cl.getResourceAsStream("../../filewebinf.properties");
- Properties prop=new Properties();
- prop.load(ins);
- System.out.println(prop.getProperty("webinf"));
- }
- //=============================================================================================
- //注意:ResourceBundle它只能讀取src下的資源(classes目錄),可以是web項目,也可以java項目
- private void test10() throws FileNotFoundException, IOException {
- ResourceBundle rb=ResourceBundle.getBundle("filesrc");//基名:不帶擴展名的src目錄下的文件
- System.out.println(rb.getString("src"));
- }
- //ResourceBundlesrc下的包中的資源,可以是web項目,也可以java項目
- private void test11() throws FileNotFoundException, IOException {
- ResourceBundle rb=ResourceBundle.getBundle("com.itheima.servletcontext.fourreadresource.filecom");//基名:不帶擴展名的src目錄下的文件
- System.out.println(rb.getString("com"));
- }
- //=====================================================================================================
- //方式一:采用ServletContext的getRealPath()讀取src目錄下的配置文件
- //這個方式只能用於web項目
- private void test00() throws FileNotFoundException, IOException {
- //獲取ServletContext對象
- ServletContext sc=getServletContext();
- //獲取文件的絕對路徑,並且絕對路徑都是以/開頭的
- String path=sc.getRealPath("/WEB-INF/classes/filesrc.properties");
- //獲取Properties對象,讀取配置文件的key-value--一下代碼都是不變的
- Properties prop=new Properties();
- InputStream ins=new FileInputStream(new File(path));
- prop.load(ins);
- System.out.println(prop.getProperty("src"));
- //prop.load(new FileInputStream(getServletContext().getRealPath("/WEB-INF/classes/filesrc.properties")));
- }
- //方式一:采用ServletContext讀取包目錄下的配置文件,只需要把路徑改一下即可
- private void test01() throws FileNotFoundException, IOException {
- //獲取ServletContext對象
- ServletContext sc=getServletContext();
- //獲取文件的絕對路徑,並且絕對路徑都是以/開頭的
- //需要把包名的.全部改成/。這是絕對路徑
- String path=sc.getRealPath("/WEB-INF/classes/com/itheima/servletcontext/fourreadresource/filecom.properties");
- //獲取Properties對象,讀取配置文件的key-value--一下代碼都是不變的
- Properties prop=new Properties();
- InputStream ins=new FileInputStream(new File(path));
- prop.load(ins);
- System.out.println(prop.getProperty("com"));
- }
- //方式一:采用ServletContext讀取WEB-INF目錄下的配置文件,只需要把路徑改一下即可
- private void test02() throws FileNotFoundException, IOException {
- //獲取ServletContext對象
- ServletContext sc=getServletContext();
- //獲取文件的絕對路徑,並且絕對路徑都是以/開頭的
- String path=sc.getRealPath("/filewebinf.properties");
- //獲取Properties對象,讀取配置文件的key-value--一下代碼都是不變的
- Properties prop=new Properties();
- InputStream ins=new FileInputStream(new File(path));
- prop.load(ins);
- System.out.println(prop.getProperty("webinf"));
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
獲取的是Toncate 項目的發布路徑