Properties
- 常用于处理配置文件
- key和value都是Sting类型
代码举例:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) throws Exception {
PropertiesTest pt = new PropertiesTest();
try {
pt.test(); //try-catch处理异常
} catch (Exception e) {
System.out.println("报错:"+e);
}
}
public void test() throws Exception{ //抛出异常
Properties prop = new Properties();
FileInputStream fs = new FileInputStream("E:\\PropertiesTestByJava.txt");
//加载流对应的文件
prop.load(fs);
String name = prop.getProperty("name"); //得到name属性的值
String password = prop.getProperty("password"); //得到password属性的值
System.out.println("name:"+name);
System.out.println("password:"+password);
fs.close(); //关闭
}
}