protected static void formatFile(String file_path,String regex,String changeStr)throws FileNotFoundException,IOException{
File f=new File(file_path);
if(!f.exists())
return ;
FileInputStream in=null;
FileOutputStream out=null;
try {
in=new FileInputStream(f);
byte[] mybyte=null;
int i= in.available();
mybyte = new byte[i];
in.read(mybyte);
in.close();
Pattern p=Pattern.compile(regex);
Matcher matcher = p.matcher(new String(mybyte));
String result= matcher.replaceAll(changeStr);
out=new FileOutputStream(file_path);
out.write(result.getBytes());
out.flush();
out.close();
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
}finally{
if(in!=null)in.close();
if(out!=null)out.close();
}
}
对文件部分内容的简单替换
最新推荐文章于 2022-08-15 13:42:44 发布