Question: Rewrite below Java code, instead of opening and closing the file 3 times I want it to be opened and closed once. package Test; import

Rewrite below Java code, instead of opening and closing the file 3 times I want it to be opened and closed once.

package Test;

import java.io.*; import java.util.regex.*;

public class Test { public static void main(String[]args) { Test l = new Test(); String[] source_system = {"CCX", "CIF", "MSB", "PTT", "SEI", "TD1", "TD2", "TD3", "TSP", "TSX", "PAP", "USR"}; try { for (int i = 0; i < source_system.length; i++) { // System.out.println(source_system.get(i)); // l.getTransactionLine(source_system[i], input_original_file_copy); l.getTransactionLine(source_system[i]); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void getTransactionLine (String transactionType) throws IOException { FileReader fr = new FileReader("sample_file.txt"); BufferedReader br = new BufferedReader(fr); String input = ""; //String input = br.readLine(); String transaction = ""; while ((input = br.readLine())!= null) { //no count function in java. So we replace all string of CCX and see if we are left with 2 times less length of CCX. if ((input.length() - input.replaceAll(transactionType, "").length())/ transactionType.length() == 2) { String [] splittedInput = input.split("\\s+"); if (splittedInput[2].equals(transactionType)) { transaction = splittedInput[0]; } //System.out.println(transaction); printTransaction(transaction); } } } private void printTransaction(String transaction) throws IOException { FileReader fr = new FileReader("sample_file.txt"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("sample_file_copy.txt", true); String input = ""; //String input = br.readLine(); int i = 1; int[]minMax = minMaxTransactionLine(transaction); while((input = br.readLine())!= null){ if (minMax[0] <= i && i <= minMax[1]) { // System.out.println(input); fw.write(input + " "); } i ++; } } private int[] minMaxTransactionLine(String transaction) throws IOException { int [] minMax = new int[2]; FileReader fr = new FileReader("sample_file.txt"); BufferedReader br = new BufferedReader(fr); String input = ""; int i = 1; int transactionStartLine = 0; int maxTransactionMatch= 0; int maxLine1Match = 0; int maxLine2Match = 0; while((input = br.readLine())!= null){ if (input.contains(transaction)) { if(transactionStartLine == 0) { transactionStartLine = i; maxTransactionMatch = i; } else if (i > maxTransactionMatch) { maxTransactionMatch = i; } } //if (Pattern.matches("-+", input)) { if(input.startsWith("----")) { //System.out.println("Reached"); if (maxLine2Match < maxLine1Match && maxLine1Match < i) { if (maxTransactionMatch==transactionStartLine) { maxLine2Match = maxTransactionMatch; } else { maxLine2Match = i; } } if (maxLine1Match < maxTransactionMatch && maxTransactionMatch < i) { maxLine1Match = i; } } i ++; } //System.out.println (transactionStartLine); //System.out.println(maxLine2Match); minMax[0] = transactionStartLine; minMax[1] = maxLine2Match;

return minMax; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!