Java Properties读写改

package cn.net.withub.xfgl.common; 

/** *//**
* @author YuLuo
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//前一篇已介绍此方法
import cn.net.withub.xfgl.common.ToUnicode;

public class CreatePro {
//读文件
public static String readFile(String filePath, String parameterName) throws IOException, FileNotFoundException {
String result = null;
File file = new File(filePath);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[fis.available()];
fis.read(b);
result = new String(b, "UTF-8");
fis.close();
}
return result;
}
//修改后存储
public static void saveFile(String content, String path, String fileName) throws IOException {
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
File fn = new File(f, fileName);
FileOutputStream fos = new FileOutputStream(fn);
fos.write(content.getBytes());
fos.close();
}
//删除旧文件
public static void deleteFile(String path) throws IOException {
File f = new File(path);
if (f.exists()) {
f.delete();
} else {
throw new IOException("未找到相关文件");
}
}
//执行方法
public static boolean writeProperties(String filePath, String parameterName, String parameterValue) {
boolean flag = false;
try {
//取得文件所有内容
String all = CreatePro.readFile(filePath, parameterName);
String result = null;
//如果配置文件里存在parameterName
if (all.indexOf(parameterName) != -1) {
//得到parameterName前的字节数
int a=all.indexOf(parameterName);
//取得以前parameterName的值
String old = readProperties(filePath, parameterName);
//得到parameterName值前的字节数
int b=a+(parameterName.length()+"=".length());
//新的properties文件所有内容为:旧的properties文件中内容parameterName+"="+新的parameterName值(parameterValue)+旧的properties文件中parameterName值之后的内容
result=all.substring(0,a)+parameterName+"="+parameterValue+all.substring(b+ToUnicode.convert(old).length(),all.length());
}
//删除以前的properties文件
CreatePro.deleteFile(filePath);
//存储新文件到以前位置
String[] arrPath = filePath.split("WEB-INF");
String path = arrPath[0]+"WEB-INF\\configs";
CreatePro.saveFile(result, path, "xf.properties");
flag=true;
} catch (IOException e) {
e.printStackTrace();
flag=false;
}
return flag;
}
//Properties方法写properties文件
/**//*Properties prop = new Properties();
try {
InputStream fis = new
FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对) prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName,parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value \n");//key
falg = true;
}catch (IOException e) {
e.printStackTrace();
System.err.println("Visit " + filePath + " for updating " + parameterName + " value error");
falg = false;
}*/
//读properties文件,Properties方法
public static String readProperties(String filePath, String parameterName) {
String value = "";
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
prop.load(fis);
value = prop.getProperty(parameterName);
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值