Java 读取配置文件

来源:互联网 发布:mac seed 编辑:程序博客网 时间:2024/04/30 01:03

配置文件的后缀是.properties

项目结构是:



读取的代码为:

/** * ./表示当前目录 ../表示父目录 /表示根目录(都是对于项目来说.而不是对于class来说) */public static void main(String[] args) throws Exception {// 读取存储的键值对Properties properties = new Properties();FileInputStream fileInputStream = new FileInputStream("./prop.properties");//该文件放在了项目的根目录properties.load(fileInputStream);fileInputStream.close();System.out.println("name:" + properties.getProperty("name"));// 已经存在的键值对System.out.println("password:" + properties.getProperty("password"));//// 不存在的键值对,不提供默认值则返回nullSystem.out.println("password1:" + properties.getProperty("password1", "password1"));// 不存在的键值对,提供默认值则返回的就是默认值// 存储和修改键值对properties.setProperty("name", "imgod");// 已经存在的话就是修改properties.setProperty("name1", "name1");// 不存在的话就是新增properties.setProperty("password", "password");// 不存在的话就是新增properties.setProperty("password1", "password额我问问");// 不存在的话就是新增FileOutputStream fileOutputStream = new FileOutputStream("prop.properties");properties.store(fileOutputStream, "update value");// 第二个参数是修改的备注信息fileInputStream.close();}


ok,配置文件的使用很简单

0 0
原创粉丝点击