代码之家  ›  专栏  ›  技术社区  ›  Shubham Jain

如何使用Java替换文件中特定行之后的字符串

  •  0
  • Shubham Jain  · 技术社区  · 6 年前

    我有一个类似的情况,如果没有找到类似的字符串,我需要在批处理文件中更改一行。

    假设我有下面这样的代码(我知道这不是正确的代码,因为它只是一个伪代码)

    public static void main(String[] args) {
    
     if (user == '1234') {
       ENV DEV
       set DB myDBDEV
       set Excel myExecelDEV
       set API MyAPIURLDEV
     } elseif (user == '5678') {
       ENV UAT
       set DB myDBUAT
       set Excel myExecelUAT
       set API MyAPIURLUAT
      }
     }
    }
    

    现在,我希望Java读取上面的文件,找到Env作为DEV,并改变MyBdDV、MyExcel、MyAPIURLDEV等的值。

    我可以使用下面的代码找到行号

           FileInputStream fis = new FileInputStream("C:\\Users\\owner\\Desktop\\batch\\MYbatch-env.csh");
            InputStreamReader input = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(input);
            String data;
            String result = new String();
            int i=0;
            while ((data = br.readLine()) != null) {
                i++;
                if(data.contains("ENV DEV")) {
                    System.out.println("line number -> "+i);
                }
                result = result.concat(data + "\n");
            }
    

    我已经尝试过下面的代码,但这不是返回行号,所以我使用上面的方法

    Finding line number of a word in a text file using java

    我也试过下面的方法,但似乎不起作用

    How to replace an string after a specific line in a file using java

    现在的问题语句是replaceall函数将删除所有键,但我想删除下一个key-means值字符串。它是一个文本字符串而不是hashmap类型的东西,

    在if block中,如果db string是mydbdev2,那么我想将值更改为mydbdev

    例子:

    如果找到以下字符串

       ENV DEV
    

    下面的值应该检查密钥db的值,如果找不到所需的值,则替换

       set DB myDBDEV
       set Excel myExecelDEV
       set API MyAPIURLDEV
    

    最主要的是代码应该只改变if块,否则if变量应该作为我在上面url中展示的一个文件示例受到影响。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Shubham Jain    6 年前

    下面的解决方案对我有用

    public static void main(String[] args) throws Exception {
            String filepath= "C:\\Users\\Demo\\Desktop\\batch\\Demo.sh";
            FileInputStream fis = new FileInputStream(filepath);
            InputStreamReader input = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(input);
            String data;
            String result = new String();
            int lineNumber=0;
            int i=0;
    
        while ((data = br.readLine()) != null) {
            i++;
            if(data.contains("My String data")) {
                System.out.println("line number -> "+i);
                lineNumber=i;
                break;
            }
            result = result.concat(data + "\n");
        }
        br.close();
        lineNumber=lineNumber+1;
        System.out.println(lineNumber);
        String Mystring ="    Mystring";
        String Mystringline = Files.readAllLines(Paths.get(filepath)).get(lineNumber-1); // get method count from 0 so -1
        System.out.println("Line data ->> "+Mystringline);
        if(!Mystringline.equalsIgnoreCase(Mystring)) {
             setVariable(lineNumber, Mystring, filepath);
        }else {
            System.out.println("Mystring is already pointing to correct DB");
        }
        System.out.println("Succesfully Change");
    }
    
    public static void setVariable(int lineNumber, String data, String filepath) throws IOException {
        Path path = Paths.get(filepath);
        List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        lines.set(lineNumber - 1, data);
        Files.write(path, lines, StandardCharsets.UTF_8);
    }
    }