Append the character of the last value to the Java list
In view of this input
0000027788|001400000000000000000001224627|G1|||G1 0000027789|001400000000000000000001224627|D1|||G1 0000027790|001400000000000000000001224627|D1|||G1 0000027790|001400000000000000000001224627|D1|||G1 0000027791|001400000000000000000001224627|G2|||G2 0000027792|001400000000000000000001224627|D2|||G2 0000027793|001400000000000000000001224627|D2|||G2 0000027794|001400000000000000000001224627|G6|||G6
In particular, I need to find out which is the last D1 of group G1 and D2 of specific G2 from column 3 in the file After finding the last value, I need to add something in the corresponding line, such as "ll":
I've tried, but this line is not attached to each D1 in parallel, not just the last D1
This is my code:
package com.scb.firstreport; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.StringTokenizer; public class EDWBatchProcessor { //static Logger log = Logger.getLogger(EDWBatchProcessor.class.getName()); public static void main(String[] args) throws JRException,NoSuchFieldException,SecurityException,IllegalArgumentException,illegalaccessexception { //log.debug("Hello this is a debug message"); File fileDir = new File("D:\\EDWFileProcessing\\simple.txt"); String line = null; String[] split = null; try { // FileReader reads text files in the default encoding. BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(fileDir),"UTF-8")); BufferedWriter bufferedWriter = null; while((line = in.readLine()) != null) { //System.out.println(line); split = line.split("\\|"); List<String> customerList = new ArrayList<String>(); if(!customerList.contains(split[1])){ customerList.add(split[1]); bufferedWriter = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("D:\\EDWFileProcessing\\output\\"+split[1]+".txt",true),"UTF-8")); bufferedWriter.write(line); bufferedWriter.newLine(); bufferedWriter.close(); } else{ bufferedWriter.write(line); bufferedWriter.close(); } } final File folder = new File("D:\\EDWFileProcessing\\output"); listFilesForFolder(folder); // Always close files. in.close(); } catch(FileNotFoundException ex) { System.out.println( "Unable to open file '"); } catch(IOException ex) { System.out.println( "Error reading file '" ); // Or we Could just do this: // ex.printStackTrace(); } } private static void listFilesForFolder(File folder) throws NoSuchFieldException,illegalaccessexception,JRException,IOException { for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { //listFilesForFolder(fileEntry); } else { // System.out.println(fileEntry.getName().substring(0,30)); //System.out.println(fileEntry.getAbsolutePath()); File fileDir = new File(fileEntry.getAbsolutePath()); String line = null; String lineNew = "000000000000000000000000000000000"; String[] split = null; // FileReader reads text files in the default encoding. BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(fileDir),"UTF-8")); BufferedWriter bufferedWriter = null; List<String> customerList = new ArrayList<String>(); List<String> recTypeList = new ArrayList<String>(); while((line = in.readLine()) != null) { // System.out.println(line); split = line.split("\\|"); bufferedWriter = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("D:\\EDWFileProcessing\\output\\simple\\"+split[1]+".txt","UTF-8")); System.out.println("Split2222222222========>>"+split[2]); System.out.println("Split2222222222========>>"+recTypeList.contains(split[2])); if(!recTypeList.contains(split[2])){ recTypeList.add(split[2]); bufferedWriter.newLine(); bufferedWriter.write(line); }else{ bufferedWriter.newLine(); line = line.concat("|LL"); bufferedWriter.write(line); System.out.println("line new....................."); //bufferedWriter.newLine(); //bufferedWriter.write(lineNew); // bufferedWriter.newLine(); } //bufferedWriter.newLine(); bufferedWriter.close(); } in.close(); } } } }
I tried to use the list, but "ll" was appended to the end of G2
for (ListIterator<String> it = recTypeList.listIterator(); it.hasNext(); i++) { String s1 = it.next(); if(s1.equals("G2")) { int ind=it.prevIoUsIndex()-1; String val=recTypeList.get(ind); String lastop=val.concat("LL"); bufferedWriter.write(lastop); System.out.println(lastop); System.out.println(val); }
Solution
The following is the method to find the last d # for each g # and add an ll at the end If we know more about format behavior, it can be simpler I'll replace it as a string, read the file and split the lines, so that when you read all the lines, you'll get the lines
public class Test { public static void main(String[] args) { String input = "0000027788|001400000000000000000001224627|G1| | |G1\r\n" + "0000027789|001400000000000000000001224627|D1| | |G1\r\n" + "0000027790|001400000000000000000001224627|D1| | |G1\r\n" + "0000027790|001400000000000000000001224627|D1| | |G1\r\n" + "0000027791|001400000000000000000001224627|G2| | |G2\r\n" + "0000027792|001400000000000000000001224627|D2| | |G2\r\n" + "0000027793|001400000000000000000001224627|D2| | |G2\r\n" + "0000027794|001400000000000000000001224627|G6| | |G6"; String[] lines = input.split("\r\n"); String[][] parts = new String[lines.length][]; for (int i = 0; i < lines.length; i++) parts[i] = lines[i].split("\\|"); String currG = "G1"; String lastD = ""; for (int i = 1; i < lines.length; i++) { if (parts[i][2].startsWith("G")) { System.out.println("Last D for " + currG + " is " + lastD + " at line " + (i-1)); lines[i-1] += " LL"; currG = parts[i][2]; } else lastD = parts[i][2]; } System.out.println(); for (int i = 0; i < lines.length; i++) System.out.println(lines[i]); } }
Output:
Last D for G1 is D1 at line 3 Last D for G2 is D2 at line 6 0000027788|001400000000000000000001224627|G1| | |G1 0000027789|001400000000000000000001224627|D1| | |G1 0000027790|001400000000000000000001224627|D1| | |G1 0000027790|001400000000000000000001224627|D1| | |G1 LL 0000027791|001400000000000000000001224627|G2| | |G2 0000027792|001400000000000000000001224627|D2| | |G2 0000027793|001400000000000000000001224627|D2| | |G2 LL 0000027794|001400000000000000000001224627|G6| | |G6
My assumption is that the second column has only g# or d#, and the 0 column is G1
Editor: if I add to my above assumption that there are only the same DS # under each g # then this is shorter:
public class Test { public static void main(String[] args) { String input = "0000027788|001400000000000000000001224627|G1| | |G1\r\n" + "0000027789|001400000000000000000001224627|D1| | |G1\r\n" + "0000027790|001400000000000000000001224627|D1| | |G1\r\n" + "0000027790|001400000000000000000001224627|D1| | |G1\r\n" + "0000027791|001400000000000000000001224627|G2| | |G2\r\n" + "0000027792|001400000000000000000001224627|D2| | |G2\r\n" + "0000027793|001400000000000000000001224627|D2| | |G2\r\n" + "0000027794|001400000000000000000001224627|G6| | |G6"; String[] lines = input.split("\r\n"); String[][] parts = new String[lines.length][]; for (int i = 0; i < lines.length; i++) parts[i] = lines[i].split("\\|"); String currG = "G1"; for (int i = 1; i < lines.length; i++) { if (parts[i][2].startsWith("G")) { System.out.println("Last D" + parts[i-1][2].substring(1) + " for " + currG + " is at line " + (i-1)); lines[i-1] += " LL"; currG = parts[i][2]; } } System.out.println(); for (int i = 0; i < lines.length; i++) System.out.println(lines[i]); } }
Edit2: read / write with file
public class Test { public static void main(String[] args) { String input = "path\\to\\input\\text.txt"; String output = "path\\to\\output\\text.txt"; BufferedReader in; BufferedWriter out; try { in = new BufferedReader(new InputStreamReader(new FileInputStream(input),"UTF-8")); out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output,"UTF-8")); String line,lastLine; lastLine = in.readLine(); while ((line = in.readLine()) != null) { String[] parts = line.split("\\|"); if (parts[2].startsWith("G")) { lastLine += " LL"; } out.write(lastLine); out.write(System.lineSeparator()); lastLine = line; } out.write(lastLine); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
Handle exceptions correctly
Here is how it works:
Lastline keeps the previous line read and searches for a new g. if one is found, lastline must contain the last d of the previous g. This is the first iteration:
lastLine: G1 0 line: D1 1 --- lastLine: D1 1 line: D1 2 --- lastLine: D1 2 line: D1 3 --- lastLine: D1 3 line: G2 4 // line starts with G,so append LL to lastLine because it's the last D for G1. --- lastLine: G2 4 line: D2 5 ...