0

I have a situation similar where I need to change a line in a batch file if similar string not found.

Suppose I have a code like below in batch(I know it is not correct code as it a dummy only)

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

Now I want java to read above file, find ENV as DEV and change the value like myDBDEV, myExecelDEV, MyAPIURLDEV etc.

I am able to find the line number by using below code

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

I have tried below code but that was not return line number so I use above approach

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

I also tried below approach but it seems not working

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

Now problem statement is replaceAll function will remove all key but I want to remove the next string of key means value. and it is a text as string not a hashmap kind thing,

In if block if DB string is myDBDEV2 then I want to change the values to myDBDEV

Example:

If below string found

   ENV DEV

Then below value should check value of key DB and replace if not found required value

   set DB myDBDEV
   set Excel myExecelDEV
   set API MyAPIURLDEV

And main thing is code should make change in if block only, else if variables should be affected as an file example I have shown in above URL.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • Seems like you just want to do something like `result = result.replace("ENV", "DEV");` for example if you wanted to change all `ENV` places to `DEV`. Then use the code in [this question you linked](https://stackoverflow.com/questions/20039980/java-replace-line-in-text-file/20040289) to write `result` back to the file. Try to actually read the code and see what it's doing (read the comments) and this is a very similar task. The three things you are doing is 1. reading the file, 2. replacing text in the file, 3. writing the file back with the replaced text. – Michael Yaworski Jun 12 '18 at 15:33
  • Yes did it and already posted as an answer – Shubham Jain Jun 12 '18 at 17:31

1 Answers1

0

Below solution work for me

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);
}
}
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125