`

java获取properties 配置文件

    博客分类:
  • java
阅读更多

 

 

根据网上的例子总结了一下。

其中cache.properties放到src下,也可以放到WEB-INF下

 

  1. package  test.bwl;   
  2.   
  3. import  java.io.FileNotFoundException;   
  4. import  java.io.IOException;   
  5. import  java.io.InputStream;   
  6. import  java.util.Properties;   
  7.   
  8. public   class  Test {   
  9.      private   static  Properties properties =  new  Properties();   
  10.   
  11.      public   static   void  main(String[] args) {   
  12.          try  {   
  13.             InputStream is = Test. class .getClassLoader().getResourceAsStream( "cache.properties" );   
  14.             properties.load(is);   
  15.             String size = properties.getProperty( "cache.size" );   
  16.             writeLog( "配置成功!"  + size);   
  17.         }  catch  (FileNotFoundException e) {   
  18.             writeLog( "配置文件不存在!"  + e.getMessage());   
  19.         }  catch  (IOException e) {   
  20.             writeLog( "读取配置文件IO错误!"  + e.getMessage());   
  21.         }   
  22.     }   
  23.   
  24.      public   static   void  writeLog(String strLog) {   
  25.         System.out.println(strLog);   
  26.     }   
  27. }  

 

 

 

 

1.使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
return rb.getString("key");

注:该方法可以读jar包里的文件

2.使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
return rb.getString("key");

3.使用java.util.Properties类的load()方法
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");

4.使用class变量的getResourceAsStream()方法
示例: InputStream in = ClassName.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");

5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = ClassName.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");

6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");

补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
return p.getProperties("key"); or return p.getProperties("key", "defaultValue");

 

7.读jar外面的文件

FileInputStream fis;

Properties p = new Properties();

    fis = new FileInputStream(initfileName);
           prop.load(fis);

注:jar文件执行时,注意cmd路径必须与jar路径相同

 

1. [代码]集中获取路径方式     

1 System.out.println("1:"+Thread.currentThread().getContextClassLoader().getResource(""));
2 System.out.println("2:" + IdcardClient.class.getClassLoader().getResource(""));
3 System.out.println("3:" + ClassLoader.getSystemResource(""));
4 System.out.println("4:" + IdcardClient.class.getResource(""));//IdcardClient.class文件所在路径
5 System.out.println("5:" + IdcardClient.class.getResource("/")); // Class包所在路径,得到的是URL对象,用url.getPath()获取绝对路径String
6 System.out.println("6:" + new File("/").getAbsolutePath());
7 System.out.println("7:" + System.getProperty("user.dir"));
8 System.out.println("8:" + System.getProperty("file.encoding"));//获取文件编码

2. [代码]Test     

1 List<String> GoodUrllists = new ArrayList<String>();
2 String[] GoodUrls =null;
3 File file = new File(UrlMatch.class.getResource("/").getFile()+"goodurls.txt");
4 GoodUrllists = FileUtils.readLines(file);
5 GoodUrls = GoodUrllists.toArray(new String[GoodUrllists.size()]);
6 System.out.println(GoodUrls.length);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics