如何快速读取制表符分隔文件?

时间:2016-02-21 15:01:55

标签: java io

我有一个函数来读取制表符分隔文件,该文件将每列放在一个列表中,并返回列表列表,其中包含列中的所有值。这适用于我用1列和1850行的小测试文件,但我现在用~30k列尝试它,它已经运行了几个小时仍然没有完成。

如何更快地修改以下代码?如果读取文件,如果30k行,1850列更快,我也可以转置输入文件。

public static List<List<String>> readTabDelimited(String filepath) {
    List<List<String>> allColumns = new ArrayList<List<String>>();
    try {
        BufferedReader buf = new BufferedReader(new FileReader(filepath));
        String lineJustFetched = null;
        for (;;) {
            lineJustFetched = buf.readLine();
            if (lineJustFetched == null) {
                break;
            }
            lineJustFetched = lineJustFetched.replace("\n", "").replace("\r", "");
            for (int i = 0; i < lineJustFetched.split("\t").length; i++) {
                try {
                    allColumns.get(i).add(lineJustFetched.split("\t")[i]);
                } catch (IndexOutOfBoundsException e) {
                    List<String> newColumn = new ArrayList<String>();
                    newColumn.add(lineJustFetched.split("\t")[i]);
                    allColumns.add(newColumn);
                }
            }
        }
        buf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return allColumns;
}

0 个答案:

没有答案
相关问题