ServletContext中getRealPath()讀取文件及其他三種讀取文件的方式及其區別


 

 

 
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

  建立好之后如圖:

  

下面是具體的代碼實現:

 

[java]  view plain  copy
 
  1. package com.itheima.servletcontext.fourreadresource;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.net.URL;  
  9. import java.util.Properties;  
  10. import java.util.ResourceBundle;  
  11.   
  12. import javax.servlet.ServletContext;  
  13. import javax.servlet.ServletException;  
  14. import javax.servlet.http.HttpServlet;  
  15. import javax.servlet.http.HttpServletRequest;  
  16. import javax.servlet.http.HttpServletResponse;  
  17.   
  18.   
  19. public class ReadPrepertiesServletContextDemo7 extends HttpServlet {  
  20.   
  21.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  22.             throws ServletException, IOException {  
  23. //      test00();  
  24. //      test01();  
  25. //      test02();  
  26. //      test10();  
  27. //      test11();  
  28. //      test21();  
  29. //      test22();  
  30. //      test23();  
  31.         test30();  
  32.   
  33.     }  
[java]  view plain  copy
 
  1. <span style="white-space:pre">    </span>//按照倒序,第四種在上面,servletcontext的getRealPath方法在最后  
  2.     //=============================================================================================  
  3.     //通過類加載器 ,默認一上來就已經定位到classes目錄  
  4.     //通過URL來獲取文件的路徑  
  5.     private void test30() throws FileNotFoundException, IOException {  
  6.         ClassLoader cl=this.getClass().getClassLoader();  
  7.         URL url=cl.getResource("filesrc.properties");  
  8.         String path=url.getPath();  
  9.         Properties prop=new Properties();  
  10.         prop.load(new FileInputStream(path));  
  11.         System.out.println(prop.getProperty("src"));//  
  12.   
  13.     }  
  14.       
  15.     //=============================================================================================  
  16.     //通過類加載器 ,默認一上來就已經定位到classes目錄  
  17.     private void test21() throws FileNotFoundException, IOException {  
  18.         ClassLoader cl=this.getClass().getClassLoader();  
  19.         InputStream ins=cl.getResourceAsStream("filesrc.properties");  
  20.         Properties prop=new Properties();  
  21.         prop.load(ins);  
  22.         System.out.println(prop.getProperty("src"));  
  23.   
  24.     }  
  25.       
  26.     //通過類加載器 ,獲取classes目錄下的包里的配置文件  
  27.     private void test22() throws FileNotFoundException, IOException {  
  28.         ClassLoader cl=this.getClass().getClassLoader();  
  29.         //此處需要把包名的.換成/  
  30.         InputStream ins=cl.getResourceAsStream("com/itheima/servletcontext/fourreadresource/filecom.properties");  
  31.         Properties prop=new Properties();  
  32.         prop.load(ins);  
  33.         System.out.println(prop.getProperty("com"));  
  34.   
  35.     }  
  36.       
  37.     //通過類加載器 ,獲取classes上上一級目錄下的配置文件  
  38.         private void test23() throws FileNotFoundException, IOException {  
  39.             ClassLoader cl=this.getClass().getClassLoader();  
  40.             //此處需要把包名的.換成/  
  41.             InputStream ins=cl.getResourceAsStream("../../filewebinf.properties");  
  42.             Properties prop=new Properties();  
  43.             prop.load(ins);  
  44.             System.out.println(prop.getProperty("webinf"));  
  45.   
  46.     }  
  47.       
  48.     //=============================================================================================   
  49.     //注意:ResourceBundle它只能讀取src下的資源(classes目錄),可以是web項目,也可以java項目  
  50.     private void test10() throws FileNotFoundException, IOException {  
  51.                 ResourceBundle rb=ResourceBundle.getBundle("filesrc");//基名:不帶擴展名的src目錄下的文件  
  52.                 System.out.println(rb.getString("src"));  
  53.   
  54.             }  
  55.     //ResourceBundlesrc下的包中的資源,可以是web項目,也可以java項目  
  56.     private void test11() throws FileNotFoundException, IOException {  
  57.         ResourceBundle rb=ResourceBundle.getBundle("com.itheima.servletcontext.fourreadresource.filecom");//基名:不帶擴展名的src目錄下的文件  
  58.         System.out.println(rb.getString("com"));  
  59.   
  60.     }  
  61.   
  62. //=====================================================================================================  
  63.   
  64.     //方式一:采用ServletContext的getRealPath()讀取src目錄下的配置文件  
  65.     //這個方式只能用於web項目  
  66.     private void test00() throws FileNotFoundException, IOException {  
  67.         //獲取ServletContext對象  
  68.         ServletContext sc=getServletContext();  
  69.         //獲取文件的絕對路徑,並且絕對路徑都是以/開頭的  
  70.         String path=sc.getRealPath("/WEB-INF/classes/filesrc.properties");  
  71.         //獲取Properties對象,讀取配置文件的key-value--一下代碼都是不變的  
  72.         Properties prop=new Properties();  
  73.         InputStream ins=new FileInputStream(new File(path));  
  74.         prop.load(ins);  
  75.           
  76.         System.out.println(prop.getProperty("src"));  
  77.     //prop.load(new FileInputStream(getServletContext().getRealPath("/WEB-INF/classes/filesrc.properties")));  
  78.   
  79.     }  
  80.     //方式一:采用ServletContext讀取包目錄下的配置文件,只需要把路徑改一下即可  
  81.     private void test01() throws FileNotFoundException, IOException {  
  82.         //獲取ServletContext對象  
  83.         ServletContext sc=getServletContext();  
  84.         //獲取文件的絕對路徑,並且絕對路徑都是以/開頭的  
  85.         //需要把包名的.全部改成/。這是絕對路徑  
  86.         String path=sc.getRealPath("/WEB-INF/classes/com/itheima/servletcontext/fourreadresource/filecom.properties");  
  87.         //獲取Properties對象,讀取配置文件的key-value--一下代碼都是不變的  
  88.         Properties prop=new Properties();  
  89.         InputStream ins=new FileInputStream(new File(path));  
  90.         prop.load(ins);  
  91.           
  92.         System.out.println(prop.getProperty("com"));  
  93.   
  94.     }  
  95.     //方式一:采用ServletContext讀取WEB-INF目錄下的配置文件,只需要把路徑改一下即可  
  96.         private void test02() throws FileNotFoundException, IOException {  
  97.             //獲取ServletContext對象  
  98.             ServletContext sc=getServletContext();  
  99.             //獲取文件的絕對路徑,並且絕對路徑都是以/開頭的  
  100.               
  101.             String path=sc.getRealPath("/filewebinf.properties");  
  102.             //獲取Properties對象,讀取配置文件的key-value--一下代碼都是不變的  
  103.             Properties prop=new Properties();  
  104.             InputStream ins=new FileInputStream(new File(path));  
  105.             prop.load(ins);  
  106.               
  107.             System.out.println(prop.getProperty("webinf"));  
  108.           
  109.   
  110.         }  
  111.   
  112.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  113.             throws ServletException, IOException {  
  114.         this.doGet(request, response);  
  115.   
  116.     }  
  117.   
  118. }  

獲取的是Toncate 項目的發布路徑

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM