0
点赞
收藏
分享

微信扫一扫

JAVA基础——properties存取

基本用法 读取

首先在项目根目录创建文件:config.properties

JAVA基础——properties存取_java
读取代码

Properties properties=new Properties();
try{
FileInputStream inputFile=new FileInputStream("config.properties"); //放在项目根目录
properties.load(inputFile);
inputFile.close();
}
catch(IOException ex){
System.out.println("加载配置文件错误!");
ex.printStackTrace();
}

获取值

properties.getProperty("port", "80");   //后一个参数是数值

其它用法

propertie.clear();                   //清除所有键值
propertie.setProperty(key, value); //设置值

保存

try{ 
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}



举报

相关推荐

0 条评论