繁体   English   中英

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

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

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

假设我有如下代码(我知道这不是正确的代码,因为它只是一个虚拟对象)

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并更改值,例如myDBDEV,myExecelDEV,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");
        }

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

使用Java查找文本文件中单词的行号

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

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

现在问题陈述是replaceAll函数将删除所有键,但我想删除键值值的下一个字符串。 它是一个文本,不是字符串,不是哈希图之类的东西,

在if块中,如果数据库字符串是myDBDEV2,那么我想将值更改为myDBDEV

例:

如果在下面找到字符串

   ENV DEV

然后,低于此值的值应检查密钥DB的值,如果找不到所需值,则应进行替换

   set DB myDBDEV
   set Excel myExecelDEV
   set API MyAPIURLDEV

最主要的是代码应该仅在if块中进行更改,否则如果变量会受到影响(如我在上面的URL中显示的文件示例)。

以下解决方案为我工作

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);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM