在JAVA的列表中追加最后一个值的字符

前端之家收集整理的这篇文章主要介绍了在JAVA的列表中追加最后一个值的字符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
鉴于此输入
  1. 0000027788|001400000000000000000001224627|G1|||G1
  2. 0000027789|001400000000000000000001224627|D1|||G1
  3. 0000027790|001400000000000000000001224627|D1|||G1
  4. 0000027790|001400000000000000000001224627|D1|||G1
  5. 0000027791|001400000000000000000001224627|G2|||G2
  6. 0000027792|001400000000000000000001224627|D2|||G2
  7. 0000027793|001400000000000000000001224627|D2|||G2
  8. 0000027794|001400000000000000000001224627|G6|||G6

我需要特别是从文件中的第3列,并找到哪个是G1组的最后一个D1,同样也是特定G2的D2.找到最后一个值后,我需要在相应的行中添加一些内容,如“LL”:

我已经尝试过,但是这条线并没有平行地附加到每个D1上,而不仅仅是最后一个D1.

这是我的代码

  1. package com.scb.firstreport;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStreamWriter;
  13. import java.util.ArrayList;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Set;
  17. import java.util.StringTokenizer;
  18. public class EDWBatchProcessor {
  19.  
  20. //static Logger log = Logger.getLogger(EDWBatchProcessor.class.getName());
  21.  
  22. public static void main(String[] args) throws JRException,NoSuchFieldException,SecurityException,IllegalArgumentException,IllegalAccessException {
  23. //log.debug("Hello this is a debug message");
  24. File fileDir = new File("D:\\EDWFileProcessing\\simple.txt");
  25. String line = null;
  26. String[] split = null;
  27. try {
  28. // FileReader reads text files in the default encoding.
  29. BufferedReader in = new BufferedReader(
  30. new InputStreamReader(
  31. new FileInputStream(fileDir),"UTF-8"));
  32.  
  33. BufferedWriter bufferedWriter = null;
  34. while((line = in.readLine()) != null) {
  35. //System.out.println(line);
  36. split = line.split("\\|");
  37.  
  38. List<String> customerList = new ArrayList<String>();
  39.  
  40. if(!customerList.contains(split[1])){
  41. customerList.add(split[1]);
  42. bufferedWriter =
  43. new BufferedWriter(new OutputStreamWriter(
  44. new FileOutputStream("D:\\EDWFileProcessing\\output\\"+split[1]+".txt",true),"UTF-8"));
  45.  
  46. bufferedWriter.write(line);
  47. bufferedWriter.newLine();
  48. bufferedWriter.close();
  49.  
  50. }
  51. else{
  52.  
  53. bufferedWriter.write(line);
  54. bufferedWriter.close();
  55. }
  56.  
  57. }
  58. final File folder = new File("D:\\EDWFileProcessing\\output");
  59. listFilesForFolder(folder);
  60. // Always close files.
  61. in.close();
  62.  
  63. }
  64. catch(FileNotFoundException ex) {
  65. System.out.println(
  66. "Unable to open file '");
  67. }
  68. catch(IOException ex) {
  69. System.out.println(
  70. "Error reading file '"
  71. );
  72. // Or we could just do this:
  73. // ex.printStackTrace();
  74. }
  75.  
  76. }
  77.  
  78. private static void listFilesForFolder(File folder) throws NoSuchFieldException,IllegalAccessException,JRException,IOException {
  79. for (final File fileEntry : folder.listFiles()) {
  80. if (fileEntry.isDirectory()) {
  81. //listFilesForFolder(fileEntry);
  82. } else {
  83. // System.out.println(fileEntry.getName().substring(0,30));
  84. //System.out.println(fileEntry.getAbsolutePath());
  85.  
  86. File fileDir = new File(fileEntry.getAbsolutePath());
  87. String line = null;
  88. String lineNew = "000000000000000000000000000000000";
  89. String[] split = null;
  90. // FileReader reads text files in the default encoding.
  91. BufferedReader in = new BufferedReader(
  92. new InputStreamReader(
  93. new FileInputStream(fileDir),"UTF-8"));
  94.  
  95. BufferedWriter bufferedWriter = null;
  96. List<String> customerList = new ArrayList<String>();
  97.  
  98. List<String> recTypeList = new ArrayList<String>();
  99.  
  100. while((line = in.readLine()) != null) {
  101. // System.out.println(line);
  102. split = line.split("\\|");
  103. bufferedWriter =
  104. new BufferedWriter(new OutputStreamWriter(
  105. new FileOutputStream("D:\\EDWFileProcessing\\output\\simple\\"+split[1]+".txt","UTF-8"));
  106.  
  107. System.out.println("Split2222222222========>>"+split[2]);
  108. System.out.println("Split2222222222========>>"+recTypeList.contains(split[2]));
  109.  
  110. if(!recTypeList.contains(split[2])){
  111. recTypeList.add(split[2]);
  112. bufferedWriter.newLine();
  113.  
  114. bufferedWriter.write(line);
  115.  
  116. }else{
  117. bufferedWriter.newLine();
  118. line = line.concat("|LL");
  119. bufferedWriter.write(line);
  120. System.out.println("line new.....................");
  121. //bufferedWriter.newLine();
  122. //bufferedWriter.write(lineNew);
  123. // bufferedWriter.newLine();
  124. }
  125.  
  126.  
  127. //bufferedWriter.newLine();
  128. bufferedWriter.close();
  129. }
  130.  
  131.  
  132. in.close();
  133.  
  134. }
  135. }
  136. }
  137.  
  138. }

我尝试使用列表,但“LL”被附加到G2的末尾.

  1. for (ListIterator<String> it = recTypeList.listIterator(); it.hasNext(); i++) {
  2.  
  3. String s1 = it.next();
  4. if(s1.equals("G2"))
  5. {
  6.  
  7. int ind=it.prevIoUsIndex()-1;
  8. String val=recTypeList.get(ind);
  9. String lastop=val.concat("LL");
  10. bufferedWriter.write(lastop);
  11. System.out.println(lastop);
  12. System.out.println(val);
  13. }

解决方法

以下是为每个G#找到最后一个D#的方法,并在最后添加一个LL.如果我们知道有关格式行为的更多信息,那么它可以变得更简单.我把它作为一个字符串替换读取文件并拆分行,这样你读完所有行后就会得到行.
  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. String input =
  6. "0000027788|001400000000000000000001224627|G1| | |G1\r\n" +
  7. "0000027789|001400000000000000000001224627|D1| | |G1\r\n" +
  8. "0000027790|001400000000000000000001224627|D1| | |G1\r\n" +
  9. "0000027790|001400000000000000000001224627|D1| | |G1\r\n" +
  10. "0000027791|001400000000000000000001224627|G2| | |G2\r\n" +
  11. "0000027792|001400000000000000000001224627|D2| | |G2\r\n" +
  12. "0000027793|001400000000000000000001224627|D2| | |G2\r\n" +
  13. "0000027794|001400000000000000000001224627|G6| | |G6";
  14.  
  15. String[] lines = input.split("\r\n");
  16. String[][] parts = new String[lines.length][];
  17. for (int i = 0; i < lines.length; i++)
  18. parts[i] = lines[i].split("\\|");
  19.  
  20. String currG = "G1";
  21. String lastD = "";
  22. for (int i = 1; i < lines.length; i++) {
  23. if (parts[i][2].startsWith("G")) {
  24. System.out.println("Last D for " + currG + " is " + lastD + " at line " + (i-1));
  25. lines[i-1] += " LL";
  26. currG = parts[i][2];
  27. }
  28. else
  29. lastD = parts[i][2];
  30. }
  31.  
  32. System.out.println();
  33. for (int i = 0; i < lines.length; i++)
  34. System.out.println(lines[i]);
  35. }
  36. }

输出

  1. Last D for G1 is D1 at line 3
  2. Last D for G2 is D2 at line 6
  3.  
  4. 0000027788|001400000000000000000001224627|G1| | |G1
  5. 0000027789|001400000000000000000001224627|D1| | |G1
  6. 0000027790|001400000000000000000001224627|D1| | |G1
  7. 0000027790|001400000000000000000001224627|D1| | |G1 LL
  8. 0000027791|001400000000000000000001224627|G2| | |G2
  9. 0000027792|001400000000000000000001224627|D2| | |G2
  10. 0000027793|001400000000000000000001224627|D2| | |G2 LL
  11. 0000027794|001400000000000000000001224627|G6| | |G6

我的假设是第二列只有G#或D#,而第0列则是G1.

编辑:如果我添加到我的上述假设,在每个G#下有Ds只有相同的#,那么这更短:

  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. String input =
  6. "0000027788|001400000000000000000001224627|G1| | |G1\r\n" +
  7. "0000027789|001400000000000000000001224627|D1| | |G1\r\n" +
  8. "0000027790|001400000000000000000001224627|D1| | |G1\r\n" +
  9. "0000027790|001400000000000000000001224627|D1| | |G1\r\n" +
  10. "0000027791|001400000000000000000001224627|G2| | |G2\r\n" +
  11. "0000027792|001400000000000000000001224627|D2| | |G2\r\n" +
  12. "0000027793|001400000000000000000001224627|D2| | |G2\r\n" +
  13. "0000027794|001400000000000000000001224627|G6| | |G6";
  14.  
  15. String[] lines = input.split("\r\n");
  16. String[][] parts = new String[lines.length][];
  17. for (int i = 0; i < lines.length; i++)
  18. parts[i] = lines[i].split("\\|");
  19.  
  20. String currG = "G1";
  21. for (int i = 1; i < lines.length; i++) {
  22. if (parts[i][2].startsWith("G")) {
  23. System.out.println("Last D" + parts[i-1][2].substring(1) + " for " + currG + " is at line " + (i-1));
  24. lines[i-1] += " LL";
  25. currG = parts[i][2];
  26. }
  27. }
  28.  
  29. System.out.println();
  30. for (int i = 0; i < lines.length; i++)
  31. System.out.println(lines[i]);
  32. }
  33. }

Edit2:带文件读写

  1. public class Test {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. String input = "path\\to\\input\\text.txt";
  6. String output = "path\\to\\output\\text.txt";
  7. BufferedReader in;
  8. BufferedWriter out;
  9. try {
  10. in = new BufferedReader(new InputStreamReader(new FileInputStream(input),"UTF-8"));
  11. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output,"UTF-8"));
  12.  
  13. String line,lastLine;
  14. lastLine = in.readLine();
  15. while ((line = in.readLine()) != null) {
  16. String[] parts = line.split("\\|");
  17. if (parts[2].startsWith("G")) {
  18. lastLine += " LL";
  19. }
  20. out.write(lastLine);
  21. out.write(System.lineSeparator());
  22. lastLine = line;
  23. }
  24. out.write(lastLine);
  25.  
  26. in.close();
  27. out.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. }

正确处理异常.

下面是它的工作原理:

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

  1. lastLine: G1 0
  2. line: D1 1
  3. ---
  4. lastLine: D1 1
  5. line: D1 2
  6. ---
  7. lastLine: D1 2
  8. line: D1 3
  9. ---
  10. lastLine: D1 3
  11. line: G2 4
  12. // line starts with G,so append LL to lastLine because it's the last D for G1.
  13. ---
  14. lastLine: G2 4
  15. line: D2 5
  16. ...

猜你在找的Java相关文章