package com.zhidi.properties;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class 写入Properties1 {
public static void main(String[] args) throws IOException {
//1.创建properties对象
Properties prop = new Properties();
//2.指定输出文件
FileOutputStream fos = new FileOutputStream("F:\\stu.properties");
//3.设置要输出的属性
prop.setProperty("userName", "yi89521");
prop.setProperty("pwd", "1984521");
//4.保存文件
prop.store(fos, "abc");
//5.关闭资源
fos.close();
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class 写入Properties1 {
public static void main(String[] args) throws IOException {
//1.创建properties对象
Properties prop = new Properties();
//2.指定输出文件
FileOutputStream fos = new FileOutputStream("F:\\stu.properties");
//3.设置要输出的属性
prop.setProperty("userName", "yi89521");
prop.setProperty("pwd", "1984521");
//4.保存文件
prop.store(fos, "abc");
//5.关闭资源
fos.close();
}
}
package com.zhidi.properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class 解析Properties12 {
public static void main(String[] args) throws IOException {
//1.实例化properties对象
Properties prop = new Properties();
//2.获得属性文件
FileInputStream fis = new FileInputStream("F:\\stu.properties");
//3.使用属性对象的load()方法获取内容
prop.load(fis);
//4.根据键获得值
String name = prop.getProperty("userName");
System.out.println(name);
String pwd = prop.getProperty("pwd");
System.out.println(pwd);
//5.关闭资源
fis.close();
}
}