7种方法获取Properties的值软件开发

本文介绍了7种在Java中获取Properties配置文件值的方法,包括使用Properties类的load方法、ResourceBundle类的getBundle方法等,并提供了详细的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7种方法获取Properties的值软件开发

Java代码
  1. import  java.io.BufferedInputStream;  
  2.   
  3. import  java.io.FileInputStream;  
  4.   
  5. import  java.io.IOException;  
  6.   
  7. import  java.io.InputStream;  
  8.   
  9. import  java.util.Properties;  
  10.   
  11. import  java.util.PropertyResourceBundle;  
  12.   
  13. import  java.util.ResourceBundle;  
  14.   
  15. import  org.apache.commons.beanutils.locale.LocaleBeanUtils;  
  16.   
  17. import  com.sun.jmx.defaults.JmxProperties;  
  18.   
  19. public   class  PropertiesUtil {  
  20.   
  21.     //  1。使用java.util.Properties 类的load()方法    
  22.   
  23.     //  示例:    
  24.   
  25.     public   static  Properties getProperties_1(String url)  throws  IOException {  
  26.   
  27.        InputStream in = new  BufferedInputStream( new  FileInputStream(url));   
  28.   
  29.        Properties p = new  Properties ();   
  30.   
  31.        p.load(in);   
  32.   
  33.        return  p;  
  34.   
  35.     }  
  36.   
  37.    
  38.   
  39.    
  40.   
  41.    
  42.   
  43.     //  2。使用java.util.ResourceBundle类的getBundle()方法    
  44.   
  45.     //  示例:   
  46.   
  47.     public   static  ResourceBundle getProperties_2(String url) {  
  48.   
  49.        ResourceBundle rb = ResourceBundle.getBundle(url, LocaleBeanUtils.getDefaultLocale());   
  50.   
  51.        return  rb;  
  52.   
  53.        //rb.getString(key)   
  54.   
  55.     }  
  56.   
  57.    
  58.   
  59.    
  60.   
  61.     //  3。使用java.util.PropertyResourceBundle类的构造函数    
  62.   
  63.     //  示例:    
  64.   
  65.     public   static  ResourceBundle getProperties_3(String url)  throws  IOException {  
  66.   
  67.        InputStream in = new  BufferedInputStream( new  FileInputStream(url));   
  68.   
  69.        ResourceBundle rb = new  PropertyResourceBundle(in);   
  70.   
  71.        return  rb;  
  72.   
  73.     }  
  74.   
  75.    
  76.   
  77.     //  4。使用class变量的getResourceAsStream()方法    
  78.   
  79.     //  示例:    
  80.   
  81.     public   static  Properties getProperties_4(String url)  throws  IOException {  
  82.   
  83.        InputStream in = JmxProperties.class .getResourceAsStream(url);   
  84.   
  85.        Properties p = new  Properties ();   
  86.   
  87.        p.load(in);  
  88.   
  89.        return  p;  
  90.   
  91.     }  
  92.   
  93.    
  94.   
  95.     //  5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法    
  96.   
  97.     //  示例:    
  98.   
  99.     public   static  Properties getProperties_5(String url)  throws  IOException {  
  100.   
  101.        InputStream in = JmxProperties.class .getClassLoader().getResourceAsStream(url);   
  102.   
  103.        Properties p = new  Properties ();   
  104.   
  105.        p.load(in);   
  106.   
  107.        return  p;  
  108.   
  109.     }  
  110.   
  111.    
  112.   
  113.    
  114.   
  115.     //  6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法    
  116.   
  117.     //  示例:    
  118.   
  119.     public   static  Properties getProperties_6(String url)  throws  IOException {  
  120.   
  121.        InputStream in = ClassLoader.getSystemResourceAsStream(url);   
  122.   
  123.    
  124.   
  125.        Properties p = new  Properties ();   
  126.   
  127.    
  128.   
  129.        p.load(in);   
  130.   
  131.        return  p;  
  132.   
  133.     }  
  134.   
  135.    
  136.   
  137.     //     
  138.   
  139.     //  补充    
  140.   
  141.     //  Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法    
  142.   
  143.     //  示例:   
  144.   
  145.     //  public static Properties getProperties_8(String url) {   
  146.   
  147.     //  InputStream in = context.getResourceAsStream(url);    
  148.   
  149.     //  Properties p = new Properties ();    
  150.   
  151.     //  p.load(in);    
  152.   
  153.     //  }   
  154.   
  155.    
  156.   
  157.     public   static   void  main(String[] args)  throws  IOException {  
  158.   
  159.        System.out.println(getProperties_1("" ));  
  160.   
  161.     }  
  162.   
  163.    
  164.   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值