package com.heilong.properties;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
*
* setProperty(key, value)
* getProperty(key)
*
* store(OutPutStream outPutStream, String message)
*
*
*
*/
public class properties {
public static void main(String[] args) throws IOException {
readProperties();
}
//读取配置文件
public static void readProperties() throws IOException {
//创建Properties对象
Properties properties = new Properties();
//加载配置文件的信息到Properties中
properties.load(new FileInputStream(new File("./src/data/property.properties")));
//遍历Properties
Set<Entry<Object, Object>> entrys = properties.entrySet();
for(Entry<Object, Object> entry : entrys){
System.out.println("键:" + entry.getKey() + " 值:" + entry.getValue());
}
}
//创建配置文件
public static void createProperties() throws IOException {
//创建Properties
Properties properties =new Properties();
properties.setProperty("admin", "123456");
properties.setProperty("root", "654321");
properties.setProperty("账户", "密码");
//使用Properties生成配置文件
properties.store(new FileOutputStream(new File("./src/data/property.properties")), "配置文件");
// properties.store(new FileWriter(new File("./src/data/property.properties")), "配置文件");
}
}
java Properties类使用方式
最新推荐文章于 2024-07-14 09:22:16 发布
2万+

被折叠的 条评论
为什么被折叠?



