properties文件就是java 中的配置文件
properties文件介绍
后缀properties的文件是一种属性文件。这种文件以key=value格式存储内容。Java中可以使用Properties工具类来读取这个文件。项目中会将一些配置信息放到properties文件中,所以properties文件经常作为配置文件来使用。
Properties工具类
Properties工具类,位于java.util包中,该工具类继承自Hashtable
Properties工具类中常用方法
load(InputStream is)
通过给定的输入流对象读取properties文件并解析
getProperty(String key)
根据key获取对应的value
注意:
如果properties文件中含有中文那么需要对idea进行设置。
import java.io.IOException;import java.io.InputStream;import java.util.Properties;//读取properties配置文件public class PropertiesTest {public static void main(String[] args) throws IOException {//实例化properties对象Properties properties = new Properties();//获取读取properties文件的输入流对象InputStream inputStream =PropertiesTest.class.getClassLoader().getResourceAsStream("test.properties");//读取配置文件properties.load(inputStream);//获取properties文件内容String value1 = properties.getProperty("key1");String value2 = properties.getProperty("key2");String value3 = properties.getProperty("key3");System.out.println(value1+value2+" "+value3);}}

