根据网上的例子总结了一下。
其中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); |
相关推荐
java如何读取properties配置文件
大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...
NULL 博文链接:https://liuzidong.iteye.com/blog/776637
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
主要介绍了java读取properties配置文件的方法,涉及java操作properties配置文件的相关技巧,需要的朋友可以参考下
NULL 博文链接:https://jeemygrow.iteye.com/blog/1162827
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
主要介绍了java简单读取properties配置文件的方法,涉及java针对properties配置的载入及文件属性读取相关操作技巧,需要的朋友可以参考下
NULL 博文链接:https://bijian1013.iteye.com/blog/2307358
下面小编就为大家带来一篇Java读取properties配置文件时,出现中文乱码的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
properties文件获取工具类:静态加载properties配置文件,有根据key获取值的方法
读取properties文件返回map
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
【Java 语言】读取 properties 配置文件 ( Java 语言中的 properties 配置文件 | 使用 properties 配置文件 ) https://hanshuliang.blog.csdn.net/article/details/134435350 博客源码
java中properties文件读取demo
通常我们的做法是用配置文件来解决。 各种语言都有自己所支持的配置文件类型。比如Python ,他支持.ini 文件。因为他内部有一个ConfigParser 类来支持.ini 文件的读写,根据该类提供的方法程序员可以自由的来操作....
主要介绍了Java读取properties配置文件的方法讲解,properties可以被看作是Java世界的ini,Java中有Properties可以操作它,需要的朋友可以参考下
Java配置文件读取,采用单例模式加载config.properties
主要介绍了Java 读取、获取配置文件.properties中的数据,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下