Given 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
I need the 3rd column, especially from the file, and find the last D1 for the group G1 and similarly the last D2 for a specific G2. After finding the last value, I need to add something to the corresponding line, for example, "LL":

I tried this, but the string joins in parallel to each D1, 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, SecurityException, IllegalArgumentException, 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",true), "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 using a list, but "LL" is added 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); }
java filereader filewriter
Anusha
source share