java FileReader FileWriter修改文件内容

java FileReader  FileWriter修改文件内容

 

public static void editContent(File fileParamOld,File fileParamNew) throws IOException{
    
    FileReader fr = new FileReader(fileParamOld);
    BufferedReader brd = new BufferedReader(fr);
    
    FileWriter fw = new FileWriter(fileParamNew);
    BufferedWriter bw = new BufferedWriter(fw);
    
    String str1="";
    String regEx="[^0-9]";
    Pattern p = Pattern.compile(regEx);
    
    while((str1=brd.readLine())!=null) {
        if(str1.contains("imagePath")) {
            String lineStr="  \"imagePath\": \"";  ///  "imagePath": "12.jpg",
            Matcher m = p.matcher(fileParamOld.getName());
            lineStr=lineStr+m.replaceAll("").trim()+".jpg"+"\",";
            System.out.println(lineStr);
            bw.write(lineStr+"\n");
        }else {
            bw.write(str1+"\n");
        }
        bw.flush();
    }
    
    bw.close();
    fw.close();
    
    brd.close();
    fr.close();
}

 

 

 

 

###############################

posted @ 2022-12-03 12:11  西北逍遥  阅读(469)  评论(0编辑  收藏  举报