我有一个类似的情况,如果没有找到类似的字符串,我需要在批处理文件中更改一行。
假设我有下面这样的代码(我知道这不是正确的代码,因为它只是一个伪代码)
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中展示的一个文件示例受到影响。