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 项目的发布路径
原文地址:https://www.cnblogs.com/nextgg/p/7794075.html