java-实现修改文件内容(2)

本篇文章主要是针对与文本特殊字符用java实现对其修改

 1 import com.rmis.common.enums.ParamType;
 2 import org.apache.commons.io.IOUtils;
 3 
 4 import java.io.*;
 5 
 6 public class FileUpdateUtils {
 7     /**
 8      * 脚本中参数替换方法
 9      *
10      * @param path      文件路径
11      * @param paramKey  参数名
12      * @param paramVal  参数值
13      * @param paramType 参数类型:数值,字符串
14      */
15     public static void replaceTxt(String path, String paramKey, String paramVal, Enum paramType) {
16         File file = new File(path);
17         BufferedReader bufIn = null;
18         FileReader in = null;
19         // 内存流, 作为临时流
20         CharArrayWriter tempStream = null;
21         FileWriter out = null;
22         try {
23             in = new FileReader(file);
24             bufIn = new BufferedReader(in);
25             // 内存流, 作为临时流
26             tempStream = new CharArrayWriter();
27             // 替换
28             String line = null;
29             while ((line = bufIn.readLine()) != null) {
30                 if (line.contains(paramKey)) {
31                     int beginIndex = 0;
32                     int endIndex = 0;
33                     beginIndex = line.indexOf("=");
34                     endIndex = line.lastIndexOf(",");
35                     String oldStr = line.substring(beginIndex + 1, endIndex);
36 
37                     //如果是字符串,手动加上单引号
38                     if(paramType.equals(ParamType.STR)){
39                         StringBuffer sb = new StringBuffer();
40                         sb.append("'").append(paramVal).append("'");
41                         paramVal = sb.toString();
42                     }
43                     line = line.replace(oldStr, paramVal);
44                 }
45                 // 将该行写入内存
46                 tempStream.write(line);
47                 // 添加换行符
48                 tempStream.append(System.getProperty("line.separator"));
49             }
50             // 将内存中的流 写入 文件
51             out = new FileWriter(path);
52             tempStream.writeTo(out);
53         } catch (FileNotFoundException e) {
54             e.printStackTrace();
55         } catch (IOException e) {
56             e.printStackTrace();
57         } finally {
58             IOUtils.closeQuietly(tempStream);
59             IOUtils.closeQuietly(out);
60             IOUtils.closeQuietly(bufIn);
61             IOUtils.closeQuietly(in);
62         }
63     }
64 
65     public static void main(String[] args) {
66         String path = "C:\\Users\\yfhx\\Desktop\\11\\bippar";
67         String paramKey ="AKQDOP";
68         String value = "22";
69         //带引号字符串 '1'
70         replaceTxt(path,paramKey,value,ParamType.STR);
71         //不带引号字符串 1
72         replaceTxt(path,paramKey,value,ParamType.NUM);
73     }
74 }
ParamType类如下:
package com.rmis.common.enums;

/**
 * @ClassName ParamType
 * @Description TODO
 * @Author xb
 * @Date 2020/06/15 19:49
 */
public enum ParamType {
    NUM,STR
}

结果如下图所示:

 

 

 

 

 结果应该很清晰,当然还有很多类似的,具体看业务需求进行不同的修改即可

posted @ 2020-07-10 14:00  执着的你  阅读(610)  评论(0编辑  收藏  举报