简体   繁体   English

在Java中解析拆分的CSV文件

[英]parsing split csv files in java

i have a csv file with comments, the values of which need to be split between two ArrayLists. 我有一个带注释的csv文件,其值需要在两个ArrayList之间分割。 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

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? 我应该使用一个计数器,该计数器在每次状态从%更改为值时都递增,反之亦然,然后如果计数器%2 = 0,则添加一个新的ArrayList并开始写入该计数器? thats the only way i can think to do it, but it seems a little bit clumsy, does anyone else have a better idea? 那是我可以想到的唯一方法,但是似乎有点笨拙,还有其他人有更好的主意吗?

EDIT: 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.. 编辑:我已经写了用于实际解析csv值的代码,我不需要帮助,只是想知道如何将这些值分成两个列表。

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. 如果文件格式确实可以预测,那么我认为任何更复杂的方法都可能会过头。

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 . 但是,如果您想使用另一种方法,这就是我的主意:每条不以%开头的行都是我们要解析为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. 因此,每当遇到非注释行时,便开始将以下行解析为ArrayList直到遇到另一个注释或文件末尾为止。 Both options mark the end of the part that you want to store in one single ArrayList . 这两个选项都标记了要存储在单个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);

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. 其中lineIsComment(String line)返回一个布尔值,该布尔值指示给定的行是否为注释,而addToList(String line, ArrayList<Integer> list)使用您的方法来解析数字行并将其存储在提供的列表中。

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

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