/***
*@Title: autoReplace 方法名
*@Description:(修改文件内容)
* @param newfilePath 新文件地址(新生成的文件地址)
* @param odlfilePath 旧文件地址(读的文件地址)
* @param oldStr 旧参数
* @param newStr 新参数
* @throws IOException(展示方法参数和返回值)
*@author:aaa
*@data 2018年10月30日
*/
private void autoReplace(String newfilePath,String odlfilePath,String[] oldStr, String[] newStr) {
PrintWriter out = null;
FileInputStream in = null;
try {
FileUtils.copyFile(new File(odlfilePath), new File(newfilePath));
File file = new File(newfilePath);
Long fileLength = file.length();
byte[] fileContext = new byte[fileLength.intValue()];
in = new FileInputStream(newfilePath);
in.read(fileContext);
String str = new String(fileContext, "utf-8");
if(oldStr.length > 0 && newStr.length > 0){
for (int i = 0; i < oldStr.length; i++) {
str = str.replace(oldStr[i], newStr[i]);
}
}
out = new PrintWriter(newfilePath);
out.write(str);
} catch (IOException e) {
WebLogs.printStackTrace(e);
} finally {
try {
out.flush();
out.close();
in.close();
} catch (IOException e) {
WebLogs.printStackTrace(e);
}
}
}
// "%1%","%2%","%3%","%5%","%6%","%7%","%8%","%4%" 分别是文件的字符
String[] oldStr ={"%1%","%2%","%3%","%5%","%6%","%7%","%8%","%4%"};
String[] newStr={mysqlname,mysqlpwd,mysqladdress,sqlsername,sqlserpwd,sqlserveraddress,sql,param};
// 执行替换方法
autoReplace(newUpdatefilePath,oldPublicFile, oldStr, newStr);
输入流与输出流更改文件内容
最新推荐文章于 2023-02-09 11:34:32 发布