简体   繁体   English

如何为每个Java运行从CSV文件解析一行,为下一次运行解析下一行?

[英]How to parse a single row from a CSV file for each java run and for the next run parse the next row?

I'm a beginner to java and I have a java class that reads in data from a CSV file which looks like this: 我是Java的初学者,我有一个Java类,它从CSV文件中读取数据,如下所示:

BUS|ID|Load|Max_Power BUS | ID |负载| Max_Power

1 | 1 | 2 | 2 | 1 | 1 | 10.9 10.9

2 | 2 | 3 | 3 | 2 | 2 | 8.0 8

My problem is this: I have to consider for each java run (program execution), only 1 row at a time. 我的问题是:我必须考虑每次Java运行(程序执行),一次只需要一行。 For example for my first run I need to read in only the first row and then for my second run I need to read in the data from the second row. 例如,对于我的第一轮,我只需要读第一行,然后对于我的第二轮,我需要读第二行的数据。 Would using Hashmaps be the right way to search for the keys for each run? 使用Hashmaps是搜索每次运行的键的正确方法吗?

when you read the 1st line (the header), you split by | 当您阅读第一行(标题)时,用|分隔 got string array ( headerArray ). 得到了字符串数组( headerArray )。 Then init a hashmap <String, List<String>> (or Multimap if you use guava or other api) with elements in your string array as key. 然后使用字符串数组中的元素作为键<String, List<String>>初始化一个哈希图<String, List<String>> (或Multimap如果您使用番石榴或其他api)。

Then you read each data row, split by | 然后,读取每个数据行,并用|分隔| , again you got string array( dataArray ), you just get the map value by: map.get(headerArray[index of dataArray]) . ,再次获得字符串array( dataArray ),您只需通过以下map.get(headerArray[index of dataArray])获取地图值: map.get(headerArray[index of dataArray]) Once you locate the map entry/value, you can do following logic (add to the list). 找到地图条目/值后,您可以执行以下逻辑(添加到列表中)。

You can also design a ValueObjectType type with those attributes, and a special setter accepting int index, String value , there you check which attribute the value should go. 您还可以设计一个具有这些属性的ValueObjectType类型,以及一个接受int index, String value的特殊设置器,在其中检查该值应归入哪个属性。 In this way, you don't need map any longer, you need a List<ValueObjectType> 这样,您不再需要映射,您需要一个List<ValueObjectType>

When your program has to "remember" something beyond it's termination, you need to store this information somewhere (file, registry, ...). 当程序必须“记住”终止之外的内容时,您需要将此信息存储在某个地方(文件,注册表等)。

Step 1 Figure out how to do file I/O (read/write files) with java. 步骤1弄清楚如何使用Java执行文件I / O(读/写文件)。 You need this to store your information, the line number in this case). 您需要使用它来存储您的信息(在这种情况下为行号)。

Step 2 Implement the logic: 步骤2实施逻辑:

  • read lineToRead from memory file (eg 1 ) 从内存文件读取lineToRead (例如1
  • read line lineToRead ( 1 ) from data file and parse the data (take a look at @Kent s answer for a nice explanation how to do so) 从数据文件中读取lineToRead1 )行并解析数据(请查看@Kent的答案,以获取有关如何执行此操作的很好的解释)
  • increment lineToRead ( 1 -> 2 ) and save it in to the memory file. 增加lineToRead1 -> 2 )并将其保存到内存文件中。

Hint: When mulitple instances of your program are going to run in parallel, you have to ensure the mutual exclusion / make the whole process (read, increment, write) atomic to prevent the lost update effect . 提示:当程序的多个实例要并行运行时,必须确保相互排斥/使整个过程(读,增量,写)原子化,以防止丢失更新效果

You can use com.csvreader.CsvReader class (available in javacsv.jar) This class provides functionality to read CSV file row by row . 您可以使用com.csvreader.CsvReader类(在javacsv.jar中可用)。此类提供了逐行读取CSV文件的功能。 This will serve your purpose . 这将达到您的目的。

here is the sample code :- 这是示例代码:-

    CsvReader csv = new CsvReader(FileName);
      csv.readHeaders(); // headers
        while (products.readRecord()) {

            String cal = csv.get(0);
        }

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

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