传智播客学习之读取配置文件
2010-01-10 00:14
387 查看
从学习javaweb以为,我们经常会遇到读取配置文件的情况,可是读取配置文件这个问题一直困扰着我,今天有一点时间,在网上搜索了一些相关的资料,整理了一下有关读取资源文件的方法,和大家共同分享。
1. 在不使用jar包时,使用如下方式读取,不失为一种方法:
File f = new File(this.getClass().getResource("/").getPath());
f = new File(f.getPath() +"/conf/config.properties");
2. 使用Apache的类库
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
PropertiesConfiguration config = new PropertiesConfiguration("file path");
config.getString("your prop name");
config.setProperty("your prop name","prop value ");
config.save();//保存.
3、使用java.util.Properties类的load()方法
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
4、使用java.util.ResourceBundle类的getBundle()方法
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
用ResourceBundle读取.properties文件可避免路径问题.
注意:process为文件名,切记不要加 .properties, URL是文件里的键名
ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");
String s = bundle.getString("URL");
System.out.println(s);
pURL = s;
5、使用java.util.PropertyResourceBundle类的构造函数
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
6、使用class变量的getResourceAsStream()方法
InputStream in = 类名.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
注意:此方法只适用于资源文件和java类在同一文件夹下使用。
7、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
InputStream in = 类名.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
8、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法,此方法灵活性很强,资源文件可以在任意路径下
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
9、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
1. 在不使用jar包时,使用如下方式读取,不失为一种方法:
File f = new File(this.getClass().getResource("/").getPath());
f = new File(f.getPath() +"/conf/config.properties");
2. 使用Apache的类库
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
PropertiesConfiguration config = new PropertiesConfiguration("file path");
config.getString("your prop name");
config.setProperty("your prop name","prop value ");
config.save();//保存.
3、使用java.util.Properties类的load()方法
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
4、使用java.util.ResourceBundle类的getBundle()方法
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
用ResourceBundle读取.properties文件可避免路径问题.
注意:process为文件名,切记不要加 .properties, URL是文件里的键名
ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");
String s = bundle.getString("URL");
System.out.println(s);
pURL = s;
5、使用java.util.PropertyResourceBundle类的构造函数
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
6、使用class变量的getResourceAsStream()方法
InputStream in = 类名.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
注意:此方法只适用于资源文件和java类在同一文件夹下使用。
7、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
InputStream in = 类名.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
8、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法,此方法灵活性很强,资源文件可以在任意路径下
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
9、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
相关文章推荐
- [spring源码学习]二、IOC源码——配置文件读取
- 学习ThinkPHP3.2.2:video9,用“C”函数读取配置文件内容
- 慕课网(imooc)Selenium自动化学习笔记02:读取配置文件及简单参数化
- spring boot学习2之properties配置文件读取
- 深入浅出学习Struts1框架(七):ActionServlet实例化之读取struts-config.xml配置文件
- 从0开始学习SpringCould(8)--SpringBoot 分环境读取配置文件
- Spring Boot学习笔记----读取配置文件
- IDEA下的MyBatis学习——读取配置文件失败的解决方法
- nginx 源码学习笔记(十三)——文件读写和配置文件读取
- Springboot学习笔记之读取全局配置文件参数
- Mybatis学习之读取配置文件(三)
- SpringBoot学习——如何设置和读取配置文件中属性
- Mybatis原理学习3:Mybatis的初始化(配置文件的读取和解析)
- nginx 源码学习笔记(十三)——文件读写和配置文件读取
- Smarty 学习笔记四 配置文件的读取
- ZendFramework学习第三章(核心组件—使用配置数据之从php配置文件中读取数据)
- java 学习笔记 读取配置文件的三种方式
- ZendFramework学习第三章(核心组件—使用配置数据之从ini配置文件中读取数据)
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- nginx 源码学习笔记——文件读写和配置文件读取