首页 > 其他 > 详细

读取resources中properties文件内容范例

时间:2019-06-23 17:11:31      阅读:98      评论:0      收藏:0      [点我收藏+]

src/main/resources下test.properties文件内容:(输入中文文件会自动转码)

name=\u674E\u78CA
age=20

src/main/java下ReadProperties.java

package properties;

import java.io.InputStream;
import java.util.Properties;

/**
 * 1 建立Properties对象
 * 2 java反射方式获取文件的流
 *   getClass():取得当前对象所属的Class对象
 *   getClassLoader():获取ReadProperties.class的类加载器
 *   Class.getClassLoader().getResourceAsStream(path): 不在同级目录下,也不在子目录下使用这种方法获取资源,最终是由ClassLoader获取资源
 * 3 Properties对象加载资源的流
 * 4 使用键值对的方式获取数据
 * 
 * Title: ReadProperties
 *
 * Description: 
 *
 * @author Ethan
 *
 * @date 2019年6月23日
 *
 */

public class ReadProperties {
    public static void main(String[] args) throws Exception {
        //建立Properties对象
        Properties prop = new Properties();
        //获取文件流
        InputStream ips = ReadProperties.class.getClassLoader().getResourceAsStream("test.properties");
        //加载文件流
        prop.load(ips);
        //获取数据
        String name = prop.getProperty("name");
        String age = prop.getProperty("age");
        System.out.println(name+":"+age);
    }
}

输出结果:

李磊:20

 

读取resources中properties文件内容范例

原文:https://www.cnblogs.com/WaterGe/p/11073092.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!