Java String.split的替代方案,可提高性能 [英] Alternative of Java String.split for better performance

查看:965
本文介绍了Java String.split的替代方案,可提高性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过从csv/tab分隔文件中导入来添加数据的过程中,我的代码花费大量时间来上传数据.有没有其他选择可以更快地执行此操作?这是我用来拆分数组中字段的代码.

In the process of adding data by import from a csv/tab seperated file, my code consumes a lot of time to upload data. Is there any alternative to do this in a more faster way ?? This is the code i use to split fields in an array.

 //Here -  lineString = fileReader.readLine()

public static String [] splitAndGetFieldNames(String lineString ,String fileType) 
{
    if(lineString==null || lineString.trim().equals("")){
        return null;
    }
    System.out.print("LINEEEE   " +  lineString);
    String pattern = "(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
    if(fileType.equals("tab"))
        pattern = "\t" + pattern;
    else
        pattern = "," + pattern;

    String fieldNames[] = lineString.split(pattern);


    for(int i=0 ; i < fieldNames.length ; i++){
        //logger.info("Split Fields::"+fieldNames[i]);
        if (fieldNames[i].startsWith("\""))
            fieldNames[i] = fieldNames[i].substring(1);
        if (fieldNames[i].endsWith("\""))
            fieldNames[i] = fieldNames[i].substring(0, fieldNames[i].length()-1);
        fieldNames[i] = fieldNames[i].replaceAll("\"\"","\"").trim();
        //logger.info("Split Fields after manipulation::"+fieldNames[i]);
    }
    return fieldNames;
}

推荐答案

使用CSV解析器,例如

Use a CSV parser like super-csv.

Univocity提供了 CSV解析器的基准.它说 univocity-parsers 很快,这不足为奇.您可以尝试一下.

Univocity provides a benchmark of CSV parsers. It says that univocity-parsers is fast, which is no surprise. You could give it a try.

这篇关于Java String.split的替代方案,可提高性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆