首页 > 解决方案 > 从以“|”分隔的 txt 文件中获取特定值

问题描述

如何从以“|”分隔的 txt 文件中获取特定值 . 我试图从第 18 列中获取值,然后我得到了,但我没有得到所有这些值。如果我取出验证 if(csvRecord.size() >= csvParser.getRecordNumber()),我会得到错误索引 18 超出长度 1 的范围。

有人已经使用过 csvParser 并且可以帮助我吗?

这是我的代码:

public static List<String> murexMHEU_TradeCash() throws Exception {
        List<String> MHEUList = new ArrayList<String>();    
        String path = Setup.MHEU_TradeCash; 
        
        
         try (  
                  BufferedReader br = new BufferedReader(new FileReader(path));
                  CSVParser csvParser = new CSVParser(br, CSVFormat.DEFAULT.withDelimiter('|'));
                 
                 
                ) {
                    //Skip first line   
                    br.readLine();
                    for (CSVRecord csvRecord : csvParser) {
                        
                        
                        if(csvRecord.size() >= csvParser.getRecordNumber()){
                            
                             // Accessing Values by Column Index
                            String MurexCounterpartyRef =  csvRecord.get(18);
                            MHEUList.add(MurexCounterpartyRef);

                        }
                        
                    }
                }
         catch(IOException e) {
             System.out.println("File not found - " + e.getMessage());
         }
        return MHEUList;
    }

标签: javacsvtxt

解决方案


推荐阅读