根据网上的例子总结了一下。
其中cache.properties放到src下,也可以放到WEB-INF下
- package test.bwl;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- public class Test {
- private static Properties properties = new Properties();
- public static void main(String[] args) {
- try {
- InputStream is = Test. class .getClassLoader().getResourceAsStream( "cache.properties" );
- properties.load(is);
- String size = properties.getProperty( "cache.size" );
- writeLog( "配置成功!" + size);
- } catch (FileNotFoundException e) {
- writeLog( "配置文件不存在!" + e.getMessage());
- } catch (IOException e) {
- writeLog( "读取配置文件IO错误!" + e.getMessage());
- }
- }
- public static void writeLog(String strLog) {
- System.out.println(strLog);
- }
- }
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); |