Code
Akshitapackage com.test.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.regex.*;
class baser {
static String fileName = "D:\\Test\\TestProject\\src\\com\\test\\controller\\file.txt";
public static void main(String[] args) {
Scanner sc = null;
try {
sc = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sc.useDelimiter("\n");
Path path = Paths.get(fileName);
int linescount = 0;
try {
linescount = (int) Files.lines(path).count();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("linescount:::" + linescount);
String[] lines = new String[linescount];
int i = 0;
while (sc.hasNext()) {
String s = sc.next();
if (s.trim().isEmpty()) {
continue;
} else {
lines[i] = s;
i++;
}
}
int startpoint = 0;
int endingpoint = 0;
for (int j = 0; j < lines.length; j++) {
if (lines[j] != null) {
boolean s1 = lines[j].contains("REPLACE==ZZZZ");
boolean s2 = lines[j].contains("REPLACE OFF");
if (s1) {
startpoint = j;
}
if (s2) {
endingpoint = j;
}
}
}
System.out.println("Starting Point:::" + startpoint);
System.out.println("Ending Point:::" + endingpoint + "\n");
for (int j = 0; j < lines.length; j++) {
if (lines[j] != null) {
String ip = lines[j];
String sb = ip;
if (j >= startpoint && j <= endingpoint) {
Pattern p = Pattern.compile("ZZZZ", Pattern.MULTILINE);
if (p.matcher(ip).find() == true) {
Matcher m = p.matcher(ip);
sb = m.replaceAll("WORK");
}
}
System.out.println(sb);
}
}
}
}