1.Properties属性文件:
1.1使用Properties读取属性文件里的键值对数据:
package specialFile;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
public class SpecialFIle {
public static void main(String[] args) throws IOException {
//创建Properties的对象,(键值对集合,空容器)
Properties pro = new Properties();
System.out.println(pro);
//加载属性文件中的键值对数据对象到Properties对象中,即pro
pro.load(new FileReader("E:\\javaproject\\src\\user.properties"));
System.out.println(pro);
System.out.println(pro.getProperty("admin1"));
//用set集合接收Properties的对象的键
Set<String> names = pro.stringPropertyNames();
//遍历全部数据
for (String key : names) {
//创建String类型对象value,接收键对应的值
String value = pro.getProperty(key);
//输出数据
System.out.println(key+"-->>"+value);
}
}
}
2.1使用Properties把键值对数据写出到属性文件里去:
package specialFile;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
//"E:\\javaproject\\src\\user.properties"
public class SpecialFIle {
public static void main(String[] args) throws IOException {
//创建Properties的对象,(键值对集合,空容器)
Properties properties = new Properties();
properties.setProperty("wwx1","lr");
properties.setProperty("wwx","nynu");
properties.setProperty("lr","mnnu");
properties.store(new FileWriter("E:\\javaproject\\src\\user1.properties"),"i set informations");
}
}