读取java配置文件

时间:2022-03-15 18:45:27
package com.io.study;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;

public class TestProperties {

//创建配置文件
Properties pp = new Properties();


/*
* 向配置文件中写入内容
*/
public void pushPro(){
FileOutputStream fos = null;

pp.setProperty("1", "你是谁?");
pp.setProperty("2", "who are you?");

try {
fos = new FileOutputStream("d:/test.properties");
pp.store(fos, "test");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("store successful!");
}


/*
*读取配置文件中的信息
*/
public void getPro(){
FileInputStream fis = null;
try {
fis = new FileInputStream("d:/test.properties");
pp.load(fis);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(pp.getProperty("1"));
System.out.println(pp.getProperty("2"));
}


/*
* 方法入口
*/
public static void main(String[] args) {
TestProperties tp = new TestProperties();
//tp.pushPro();
tp.getPro();
}
}