在java中解析split csv文件 [英] parsing split csv files in java

查看:235
本文介绍了在java中解析split csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有注释的csv文件,其值需要在两个ArrayList之间拆分。例如:

i have a csv file with comments, the values of which need to be split between two ArrayLists. eg:

% the values below here should go
% into ArrayList<Integer> list1
3,4,5,2,2,3
5,6,3,2,4,5
3,2,3,4,5,6
2,3,4,5,1,3
% the values below here should go
% into ArrayList<Integer> list2
4,6,3,4,5,3
3,4,5,6,3,2
4,5,6,4,3,2

这是最好的方法是什么?应该使用一个计数器,每当状态从%变化到一个值或反之亦然,然后如果计数器%2 = 0,然后添加一个新的ArrayList并开始写入?这是我能够想到的唯一的方式,但它似乎有点笨拙,其他人有更好的主意吗?

what is the best way of achieving this? should i use a counter that is incremented each time the state changes from % to a value or vice versa and then if counter % 2 = 0 then add a new ArrayList and start writing to that? thats the only way i can think to do it, but it seems a little bit clumsy, does anyone else have a better idea?

编辑:我已经写了代码为了实际解析csv值,我不需要帮助,只是想知道如何将值拆分成两个列表。

i have already written the code for actually parsing the csv values, i dont need help with that, just wondering about how to split the values into two lists..

推荐答案

您的建议对我听起来不错。如果文件格式真的如此可预测,任何更复杂的方法可能只是在我看来overkill。

Your suggestion sounds good to me. If the file format is really so predictable, any more sophisticated approaches might just be overkill in my opinion.

但是如果你想看到一个不同的方法,这是我的想法:每行不开始是我们要解析为 ArrayList 的东西。所以,每当你遇到一个不是注释的行,你开始解析以下行到 ArrayList ,直到你打另一个注释或文件的结尾。两个选项都将要存储在零件的结尾标记为一个 ArrayList 。所以,这样的:

But if you want to see a different approach, this is my idea: Every line that does not start with an % is something we want to parse to an ArrayList. So, whenever you come across a line that is not a comment, you start parsing the following lines to an ArrayList until you either hit another comment or the end of the file. Both options mark the end of the part that you want to store in one single ArrayList. So, something like this:

ArrayList<ArrayList<Integer>> arrays = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> currentArray;
while(inputStream.hasNextLine()) {
    String line = inputStream.getLine();
    if(!lineIsComment(line)) {
        // This means that we are in a number block. We store the numbers
        // either in an existing list or create a new one if necessary
        if(currentArray == null) {
            currentArray = new ArrayList<Integer>();
        }
        addToList(line, currentArray);
    } else if(currentArray != null) {
        // In this case a comment block starts and currentArray contains
        // the numbers of the last number block.
        arrays.add(currentArray);
        currentArray = null;
    }
}
if(currentArray != null) arrays.add(currentArray);

其中 lineIsComment(String line)布尔值,表示给定的行是否为注释, addToList(String line,ArrayList )列表使用您的方法解析一行数字,提供的列表。

where lineIsComment(String line) returns a boolean that indicates whether the given line is a comment, and addToList(String line, ArrayList<Integer> list) uses your method to parse a line of numbers and stores them in the provided list.

这篇关于在java中解析split csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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