为 JAVA 列表中的最后一个值附加一个字符

Append a character for last value in a list in JAVA

鉴于此输入

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

我特别需要文件中的第 3 列,并找到组 G1 的最后一个 D1 和特定 G2 的最后一个 D2。找到最后一个值后,我需要在相应的行中附加一些内容,例如 "LL":

我试过了,但是这条线被平行地附加到每个 D1,而不仅仅是最后一个 D1。

这是我的代码:

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();  

        }
   }
}

}

我尝试使用列表,但是 "LL" 被附加到 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);
                 }

以下是如何找到每个 G# 的最后一个 D# 并在末尾附加一个 LL。如果我们知道有关格式行为方式的更多信息,它可能会变得更简单。我通过将文件作为字符串并拆分行来代替读取文件,这样 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]);
    }
}

输出:

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

我的假设是第二列只有 G#D# 并且在第 0 行是 G1.

编辑: 如果我在上面的假设中添加,在每个 G# 下有 Ds 只有相同的 # 然后这个更短:

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:带文件读写

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, true), "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();
        }
    }
}

正确处理异常。

这是它的工作原理:

lastLine 保持读取前一行,而 line 搜索新的 G。如果找到一个,则 lastLine 必须包含 previous G 的最后一个 D。这是第一次迭代:

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
...

首先,您需要对文件进行两次遍历;一个是确定每个 GX 的每个 Last line 在哪里,然后在那里添加 LL。但是,我认为如果我们知道您的流程是什么以及为什么需要将其放在那里,我们可能会提供更多帮助。

无需保留行列表 - 如果您正在处理非常大的文件,这可能很重要。您需要的是在读取文件时存储上一行,并将其写入 "one line behind".

您可以从 findFilesForFolder 中删除 customerList(无论如何您都不会使用)和 recTypeList,然后只添加一个字符串。

我已经使用了您的部分代码并添加了一些我自己的行来说明我的意思:

        String previousLine = null;
        String[] previousSplit = null;
        while((line = in.readLine()) != null) {
            split = line.split("\|");

            // ...

            if (previousLine != null) {
                bufferedWriter.write(previousLine);
                if (!previousSplit[2].equals(split[2])) {
                    bufferedWriter.write("LL");
                }
                bufferedWriter.newLine();

                // ...

            }
            previousLine = line;
            previousSplit = split;
        }
        bufferedWriter.write(previousLine);
        bufferedWriter.write("LL");
        bufferedWriter.newLine();

(以上代码只是说明了该技术;您必须在自己的代码中添加更多处理以检查最后一列 - 组 - 并对每个组独立执行上述操作。)

如果您的输入文件按 G 和 D 列排序,您可以一次性完成:

public void processSorted() throws Exception {
    reset();

    previousG = null;

    while (next()) {
        finishLastLine();
        out.print(line);
        previousDep = values[2];
        previousG = group();
    }

    finishLastLine();
}

private void finishLastLine() {
    if (previousG != null && previousDep != null) {
        if (!group().equals(previousG) || !dep().equals(previousDep)) {
            if (previousG.equals("G1") && previousDep.equals("D1")) {
                out.print("|LL");
            } else if (previousG.equals("G2") && previousDep.equals("D2")) {
                out.print("|LL");
            }
        }
        out.println();
    }
}

如果没有排序,你首先需要找到D1和D2的最后一条记录(你需要到达终点告诉它),然后再次读取输入写入输出文件(除非你是告诉处理时间很重要,总是有足够的内存来处理输入):

public void scanUnsorted() throws Exception {
    reset();
    while (next()) {
        switch (group()) {
            case "G1":
                if (dep().equals("D1"))
                    lastD1 = index;
                break;
            case "G2":
                if (dep().equals("D2"))
                    lastD2 = index;
                break;
        }
    }
}

public void write() throws Exception {
    reset();
    while (next()) {
        out.print(line);
        if (lastD1 >= 0 && index == lastD1
         || lastD2 >= 0 && index == lastD2
        ) {
            out.print("|LL");
        }
        out.println();
    }
    out.flush();
}

这里是 the full runnable gist 测试用例。