读取资源文件方法

  1 package cn.gs.ly;
  2 import java.io.FileInputStream;
  3 import java.io.FileNotFoundException;
  4 import java.io.IOException;
  5 import java.io.InputStream;
  6 import java.util.Properties;
  7 import java.util.ResourceBundle;
  8 import javax.servlet.ServletContext;
  9 import javax.servlet.ServletException;
 10 import javax.servlet.http.HttpServlet;
 11 import javax.servlet.http.HttpServletRequest;
 12 import javax.servlet.http.HttpServletResponse;
 13 
 14 /**
 15  * 读取资源文件
 16  *    1.ServletContext.getRealPath() 可读取应用中的任何文件,但只能在web中使用。
 17  *    2.ResourceBundle 可以在非web中使用,但是只能读取类路径(src)中的properties文件。
 18  *    3.利用类加载器ClassLoader 可以在非web环境中使用,可以读取类路径下任何文件。
 19  *  a.properties:E:WorkspaceWeb017WebContentWEB-INFa.properties
 20  *  b.properties:E:WorkspaceWeb017src.properties
 21  *  c.properties:E:WorkspaceWeb017srccngslyc.properties
 22  * */
 23 public class ServletPro extends HttpServlet{    
 24     @Override
 25     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 26         //测试方法
 27         test1();
 28 //        test2();
 29 //        test3();
 30 //        test4();
 31 //        test5();
 32 //        test6();
 33 //        test7();
 34     }
 35     @Override
 36     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 37         this.doGet(req, resp);
 38     }
 39 //========================== 使用getRealPath() ===============================================================    
 40     //使用getRealPath()读取a.properties
 41     private void test1() throws FileNotFoundException, IOException{
 42         ServletContext sc = this.getServletContext();
 43         String path = sc.getRealPath("/WEB-INF/a.properties");
 44         Properties pro = new Properties();        
 45             pro.load(new FileInputStream(path));
 46             String value = pro.getProperty("username");
 47             System.out.println(value);        
 48     }
 49     //使用getRealPath()读取b.properties
 50     private void test2() throws FileNotFoundException, IOException{
 51         ServletContext sc = this.getServletContext();
 52         String path = sc.getRealPath("/WEB-INF/classes/b.properties");
 53         Properties pro = new Properties();        
 54             pro.load(new FileInputStream(path));
 55             String value = pro.getProperty("username");
 56             System.out.println(value);        
 57     }
 58     //使用getRealPath()读取c.properties
 59     private void test3() throws FileNotFoundException, IOException{
 60         ServletContext sc = this.getServletContext();
 61         String path = sc.getRealPath("/WEB-INF/classes/cn/gs/ly/c.properties");
 62         Properties pro = new Properties();        
 63             pro.load(new FileInputStream(path));
 64             String value = pro.getProperty("username");
 65             System.out.println(value);        
 66     }
 67     
 68 //======================== 使用ResourceBundle对象 =================================================================        
 69     //使用ResourceBundle对象读取b.properties
 70     private void test4(){
 71         ResourceBundle rb = ResourceBundle.getBundle("b");  // 基名
 72         String value = rb.getString("username");
 73         System.out.println(value);
 74     }
 75     //使用ResourceBundle对象读取c.properties
 76     private void test5(){
 77         ResourceBundle rb = ResourceBundle.getBundle("cn.gs.ly.c");  // 基名
 78         String value = rb.getString("username");
 79         System.out.println(value);
 80     }        
 81     
 82 //======================= 利用类加载器ClassLoader ==================================================================        
 83     //利用类加载器ClassLoader  读取b.properties文件
 84     private void test6() throws IOException{
 85         //Class c = ServletPro.class;
 86         //c.getClassLoader();
 87         ClassLoader c = ServletPro.class.getClassLoader();
 88         InputStream in = c.getResourceAsStream("b.properties");
 89         Properties pro = new Properties();
 90         pro.load(in);
 91         String value = pro.getProperty("username");
 92         System.out.println(value);            
 93     }
 94     //利用类加载器ClassLoader  读取c.properties文件
 95     private void test7() throws IOException{
 96         //Class c = ServletPro.class;
 97         //c.getClassLoader();
 98         ClassLoader c = ServletPro.class.getClassLoader();
 99         InputStream in = c.getResourceAsStream("cn/gs/ly/c.properties");
100         Properties pro = new Properties();
101         pro.load(in);
102         String value = pro.getProperty("username");
103         System.out.println(value);            
104     }
105     
106 }
原文地址:https://www.cnblogs.com/liuyangv/p/8182894.html