对文件的操作,例:properties
程序员文章站
2024-03-17 11:51:46
...
/**
* 操作配置文件
* @author LuoPiao
*
*/
public class MyProperties {
//创建文件
public static boolean createProperties(String filePath,String name,String password,String money){
OutputStream out=null;
InputStream in=null;
try {
//文件不存在则会自动创建
out = new FileOutputStream(filePath);
Properties pps = new Properties();
in = new FileInputStream(filePath);
pps.load(in);//注意:相当于map集合,无序
//添加三个属性,左边键,右边值
pps.setProperty("name", name);
pps.setProperty("password", password);
pps.setProperty("money", money);
pps.store(out, "sign in");
return true;
} catch (IOException e) {
return false;
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//根据Key读取Value
public static String getValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
in.close();
return value;
}catch (IOException e) {
e.printStackTrace();
return null;
}
}
//写入Properties信息
public static void writeProperties (String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties();
InputStream in = new FileInputStream(filePath);
pps.load(in);
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
pps.store(out, "Upate"+pKey);
out.close();
in.close();
}
}
上一篇: JS--变速动画函数封装增加任意多个属性
下一篇: 原生js封装函数
推荐阅读
-
对文件的操作,例:properties
-
对文件的操作,例:txt文件
-
java的输入输出部分,及文件操作
-
java的输入输出流与文件操作(2.读入写出及关流)
-
python文件的操作
-
java中载入properties文件的方法 博客分类: core java javaproperties载入文件
-
java中载入properties文件的方法 博客分类: core java javaproperties载入文件
-
linux操作系统下安装mysql时的默认配置文件设置
-
对mongo文档的增删改操作 博客分类: mongodb学习 mongodbmongodb文档增删改操作
-
五、hibernate中持久层操作(多表之间的关联关系操作,1对1,一对多,多对多、继承)