I have a .txt file which i want to read using Java and then verify if some particular fields is equal to a given string.
Now, i want to validate that if line/record starts with 03, then see of 7th index field is '0420'. Also, if its an empty field, then verify it.
public class ReadFile {
public static List<String> lines;
public static void main(String[] args) throws IOException {
readFileIntoList("File1.txt");
String s1 = lines.get(0).subString(0,7)
if(s1.equals("0420")){
sop(s1);
}
}
public static List<String> readFileIntoList(String file) {
lines = Collections.emptyList();
try {
lines = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
for (String s : lines) {
System.out.println(s);
}
} catch (IOException e) {
}
return lines;
}
}
File1.txt
01,23434,34343,6335,1235533,345,,,42/
02,TEST23,2253434,1,2233,2233,,4/
03,TEST44,,1123,3434343,,,0420,3434,,,/
04,2343,24334,Test2,1600819,4802458,3201638,NOTE1,TEXT2,
66,MORE TEXT IN THIS LINE
04,2343443,24334,Test2,1600819,4802458,3201638,NOTE122,TEXT2,
public static void main(String[] args) throws IOException {
String col_0 = "03";
String col_7 = "0420";
boolean matchedFound = false;
List<String> readAllLines = readFileIntoList();
for (int i = 0; i < readAllLines.size(); i++) {
String[] splittedLine = readAllLines.get(i).split(",");
if(splittedLine.length >= 7) {
String firstWord = splittedLine[0];
if(firstWord.equals(col_0)) {
String seventhWord = splittedLine[7];
if(seventhWord.equals(col_7)) {
matchedFound = true;
break;
}
}
}
}
System.out.println(matchedFound);
}