简体   繁体   English

在Java中拆分一个字符串并将其插入一个空字符串

[英]Split a string in Java and insert it into an empty string

I have a CSV file with the following data:我有一个包含以下数据的 CSV 文件:

20210903|0000000001|0081|A|T60|BSN|002|STATE UNITED

I have imported this file in my java application with this code:我已使用以下代码将此文件导入到我的 Java 应用程序中:

public List<EquivalenceGroupsTO> read() throws FileNotFoundException, IOException {

    try (BufferedReader br = new BufferedReader(new FileReader("/home/myself/Desk/blaBla/T60.csv"))) {

        List<String> file = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        Integer count = 0;
        HashSet<String> hset = new HashSet<String>();

        while (line != null) {
            //System.out.println("data <" + count + "> :" + line);
            count++;
            file.add(line);
            file.add("\n");
            line = br.readLine();
        }

        EquivalenceGroupsTO equivalenceGroupsTO = new EquivalenceGroupsTO();

        List<EquivalenceGroupsTO> equivalenceGroupsTOs = new ArrayList<>();

        for (String row : file) {
            equivalenceGroupsTO = new EquivalenceGroupsTO();
            String[] str = row.split("|");
            equivalenceGroupsTO.setEquivalenceGroupsCode(str[5]);
            equivalenceGroupsTO.setDescription(str[7]);
            equivalenceGroupsTO.setLastUpdateDate(new Date());
            equivalenceGroupsTOs.add(equivalenceGroupsTO);
            System.out.println("Tutto ok!");
        }
        return equivalenceGroupsTOs;
    }
}

I need to set in the equivalenceGroupsTO.setEquivalenceGroupsCode and in the equivalenceGroupsTO.setDecription (which are strings) respectively the strings after the fifth and the seventh "|"我需要在equivalenceGroupsTO.setEquivalenceGroupsCodeequivalenceGroupsTO.setDecription (它们是字符串)中分别设置第五个和第七个“|”之后的字符串, then " BSN " and " STATE UNITED ". ,然后是“ BSN ”和“ STATE UNITED ”。

But if I start this script it gives me this error:但是如果我启动这个脚本,它会给我这个错误:

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 1
at it.utils.my2.read(OpenTXTCodifa.java:46)

What am I doing wrong?我究竟做错了什么?

Main issue is mentioned in the comments: when splitting by |评论中提到了主要问题:当拆分时| character, it has to be escaped as \\\\|字符,它必须转义为\\\\| because the pipe character is user as OR operator in the regular espressions.因为管道字符在常规表达式中是用户作为 OR 运算符。

Next issue is adding a line containing only \\n to file .下一个问题是将仅包含\\n的行添加到file When this line is split, str[5] will fail with ArrayIndexOutOfBoundsException .当这条线被拆str[5]str[5]将失败并显示ArrayIndexOutOfBoundsException

Other minor issues are unused variables count and hset .其他小问题是未使用的变量counthset

However, it may be better to refactor existing code to use NIO and Stream API to get a stream of lines and convert each line into corresponding list of EquivalenceGroupsTO :但是,重构现有代码以使用 NIO 和 Stream API 获取行流并将每一行转换为对应的EquivalenceGroupsTO列表可能会更好:

public List<EquivalenceGroupsTO> read(String filename) throws IOException {
    return Files.lines(Paths.get(filename)) // Stream<String>
            .map(s -> s.split("\\|"))       // Stream<String[]>
             // make sure all data are available
            .filter(arr -> arr.length > 7)  // Stream<String[]>
            .map(arr -> {
                EquivalenceGroupsTO egTo = new EquivalenceGroupsTO();
                egTo.setEquivalenceGroupsCode(str[5]);
                egTo.setDescription(str[7]);
                egTo.setLastUpdateDate(new Date());
                return egTo;
            }) // Stream<EquivalenceGroupsTO>
            .collect(Collectors.toList())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM