前提条件:
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); } }