JAVA读取propertise配置文件

java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"key=value"的格式,在properties

文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便。

一,properties文件

   test.properties

001=hello
002=world

二,JAVA代码,通过key值得到value值

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


public class TestProperties {
    public static void main(String[] args) {
        //创建Properties对象
        Properties p = new Properties();
        //得到test.properties文件的流入流
        InputStream is = TestProperties.class.getResourceAsStream("test.properties");
        try {
            //加载文件--通过文件输入流
            p.load(is);
            //关闭输入流
            is.close();
            //通过key值得到value值
            String value = p.getProperty("002");
            System.out.println(value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  输出结果:world

原文地址:https://www.cnblogs.com/likailan/p/3252608.html