java读取properties文件的方法

盗亦有道:

http://blog.csdn.net/lwzcjd/article/details/3116298

http://hi.baidu.com/hgd0324/item/1d5e923973b77c4d033edcaf

这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件

构造如下config.properties文件properties代码

userDao.class=com.spring.dao.UserDao

属性文件中的"userDao"名称即是Bean的别名设定,.class用于指定类来源。

然后利用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader来读取属性文件
   BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
   PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(reg);
   reader.loadBeanDefinitions(new ClassPathResource("config.properties"));
   BeanFactory factory = (BeanFactory)reg;
   UserDao userDao = (UserDao)factory.getBean("userDao");

(二)利用java.util.Properties读取属性文件
1.    String str=File.separator;
        InputStream path=this.getServletContext().getResourceAsStream(str+"WEB-INF"+str+"classes"+str+"password.properties");

        //InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("password.properties");

    /*File filepath=new File(this.getServletContext().getRealPath(str+"WEB-INF"+str+"classes")+str+"password.properties");

        InputStream path=new FileInputStream(filepath);*/
        Properties pros = new Properties();
        try {
            pros.load(path);
        } catch (IOException ex) {
            //System.out.println("file is not exist");
            errorMessage="资源文件不存在";
        }
        System.out.println("username:"+p.getProperty("username")+",password:"+p.getProperty("password"));

2.    import org.springframework.core.io.ClassPathResource;

        ClassPathResource cr = new ClassPathResource("password.properties");//会重新加载spring框架
        Properties pros = new Properties();
        try {
            pros.load(cr.getInputStream());
        } catch (IOException ex) {
            //System.out.println("file is not exist");
            errorMessage="资源文件不存在";
        }

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());

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

6.使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

7.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.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);

java读取properties文件的方法

时间: 2024-07-30 10:06:44

java读取properties文件的方法的相关文章

Java读取Properties文件的六种方法

使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2.使用java.util.ResourceBundle类的getBundle()方法 示例: ResourceBundle rb

转载:java基础学习总结——java读取properties文件总结

java基础学习总结--java读取properties文件总结 一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的

用java读取properties文件--转

今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest public static void readFile(String fileName) {//传入参数fileName是要读取的资源文件的文件名如(file.properties) InputStream in = null; Properties pros = new Properties(); tr

java基础学习总结——java读取properties文件总结

一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的目录结构 1.2. java读取properties文件代码测试

Java读取.properties文件

例1: 创建一个config文件夹 config文件夹中有一个Properties.properties文件 内容为: capitalLetter=ABCDE smallLetter=abcde 注意:config文件夹与包含Test类的包为同一级 import java.io.IOException; import java.util.Properties; public class Test { public static void main(String[] args) { Propert

java读取.properties文件乱码

1.config.properties文件写不进中文,写进去都变成了unicode,解决办法是右键该文件--Properties--Resource--Text file encoding ,选other,我将other改为了UTF-8,这样可以写进去中文,但是读取时又变成乱码了. 2,解决读取乱码: String content = new String(PropertiesConfig.getProperty("mail.content").getBytes("ISO88

java读取TXT文件的方法

java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了.接下来需要解读成乙方可以理解的东西 既然你使用了FileInputStream().那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据 解读完成后要输出

java读取TXT文件的方法 (转)

转自:http://www.cnblogs.com/manongxiaojiang/archive/2012/10/13/2722068.html java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了.接下来需要解读成乙方可以理解的东西 既然你使用了F

java读取.properties文件及解决中文乱码问题

Java项目中有些信息(例如web的配置信息)可能要放在.properties文件中,那我们应该怎么来读取它们呢,下面给出一个工具类做一说明,并解决了中文乱码问题: 1.其中config.properties文件信息如下: name=\u843D\u82B1\u6709\u610Fwang王 str=\u6D41\u6C34\u65E0\u60C5 boolean=true 2.PropertiesUtil工具类读取.properties文件 import java.io.BufferedInp