-2

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

注意: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

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

ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
ABC
4

2 回答 2

1

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

while ((st = Br.readLine()) != null) {
    arraylist = st.split("\\*");
    System.out.println("List value are " + arraylist[1]);
}
于 2018-12-20T20:59:50.217 回答
0

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

   // 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");
            }
   }
于 2018-12-20T20:36:08.860 回答