存储在数组列表中时,星号 * 分隔的文本文件列返回多次列值

Star * delimiter txt file column is returning multiple times column value when stored in Array List

我有星号 * 分隔符文件,我最终需要在拆分 * 分隔符后将其存储在数组中,它在数组中存储值并打印列,但是它不断地一次又一次地打印相同的列值。

注意:txt文件中有60列。

以下是我的代码:

static String[] arraylist;

String st;
            BufferedReader Br = null;


            File objFile = new File("C://DATA//File.txt");

            Br = new BufferedReader(new FileReader(objFile));

            LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(objFile));
            lineNumberReader.skip(Long.MAX_VALUE);
            int lines = lineNumberReader.getLineNumber();


            System.out.println("Total Rows in the File : " + lines);

            List<String> lis = new ArrayList<String>();

            while ((st = Br.readLine()) != null) {

                arraylist = st.split("\*");

                for (int i = 0; i < arraylist.length; i++) {

                    lis.add(arraylist[1]);
                    lis.add("\n");

                    System.out.println("List value are " +lis);

                }

            }


            Br.close();

以下是列示例:(同样,此文件中有 60 列)

*ABC*08*31444*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*66657*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*33161*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*55771*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*20605*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*26471*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*85608*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*57735*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*46844*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*57823*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*31249*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*32394*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*46166*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*46167*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*43108*XYZ*1234*OLNBHJK*^TGJF54DFG908
*ABC*08*43110*XYZ*1234*OLNBHJK*^TGJF54DFG908

我只想打印第 2 列一次:例如:

ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC

以上代码每行只打印两次第三个标记。这是更正后的代码

   // the reading code is the same
   while ((st = Br.readLine()) != null) {
            String[] tokens = st.split("\*");

            for (int i = 0; i <= tokens.length; i++) {
                lis.add(tokens[i]);
                lis.add("\n");
            }
   }

您需要做的就是打印数组中的第二项。请注意,数组索引从 0 开始。

while ((st = Br.readLine()) != null) {
    arraylist = st.split("\*");
    System.out.println("List value are " + arraylist[1]);
}