---
title: "java获取properties 配置文件"
author: "上海-沉默者"
date: "2010-10-23T10:49:33Z"
tags: ["Cache", "Java", "Servlet", "Web"]
categories: ["编程语言"]
slug: "java-properties"
draft: false
lastmod: "2010-10-23T10:50:19Z"
id: 792002
url: "https://liuwanxiang.iteye.com/blog/792002"
---

根据网上的例子总结了一下。

其中cache.properties放到src下，也可以放到WEB-INF下
4.package  test.bwl;    
5.   
6. import  java.io.FileNotFoundException;    
7. import  java.io.IOException;    
8. import  java.io.InputStream;    
9. import  java.util.Properties;    
10.   
11.public   class  Test {    
12.     private   static  Properties properties =  new  Properties();    
13.   
14.     public   static   void  main(String[] args) {    
15.         try  {    
16.            InputStream is = Test. class .getClassLoader().getResourceAsStream( "cache.properties" );    
17.            properties.load(is);    
18.            String size = properties.getProperty( "cache.size" );    
19.            writeLog( "配置成功！"  + size);    
20.        }  catch  (FileNotFoundException e) {    
21.            writeLog( "配置文件不存在！"  + e.getMessage());    
22.        }  catch  (IOException e) {    
23.            writeLog( "读取配置文件IO错误！"  + e.getMessage());    
24.        }    
25.    }    
26.   
27.     public   static   void  writeLog(String strLog) {    
28.        System.out.println(strLog);    
29.    }    
30.}   
 

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路径相同 


