在java中读取csv文件从第3行开始

发布于 2024-09-30 14:04:17 字数 164 浏览 1 评论 0原文

我如何让我的csv文件阅读器将java中的csv文件读取到数组列表中,从第3行开始,但第1行和第2行,每个都被读取到另一个数组列表。例如:

line1 ->数组列表1;
第2行->数组列表2;
line3 - 第 n 行 ->数组列表3

谢谢..

how do i make my csv file reader to read my csv file in java to an arraylist, start from line no.3 but line1 and line2, each of them is read to another arraylist. for example:

line1 -> arraylist1;
line2 -> arraylist2;
line3 - line'n' -> arraylist3

thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红颜悴 2024-10-07 14:04:17

从有限的细节来看,一个简单的实现将执行以下操作:

  1. 打开 CSV 文件
  2. 读取第一行
    1. 处理第一行,并创建一个列表。
  3. 读第二行
    1. 处理第一行,并创建一个列表。
  4. 阅读下一行
    1. 处理该行并创建一个列表。
  5. 重复(4)直到文件末尾。
  6. 关闭 CSV 文件。

我可能会通过单独的方法来处理步骤 2-1、3-1 和 4-1,因为我猜测这 3 个步骤的格式彼此不同。

文件的实际读取可能使用 BufferedReader 能够使用 readLine 方法,但这部分可能会根据应用程序的设计而有所不同。

该行的处理可以通过将行分割来完成 String.split(",") 并将结果扔进 List 中,但它会很脆弱并且容易出错,因此需要一种更好的方法来分解组件,或者使用可以正确处理边缘情况的 CSV 操作库可能是更好的方法。

From the limited details, a simple implementation would do the following:

  1. Open the CSV file
  2. Read the first line
    1. Process the first line, and create a list.
  3. Read the second line
    1. Process the first line, and create a list.
  4. Read the next line
    1. Process the line, and create a list.
  5. Repeat (4) until the end of the file.
  6. Close the CSV file.

I would probably approach step 2-1, 3-1 and 4-1 by having separate methods, as I am guessing that the formatting for each of those 3 steps are different from each other.

The actual reading of the file may be using a BufferedReader which is able to read an entire line at a time by using the readLine method, but this part may vary depending on the design of the application.

The processing of the line may be accomplished by splitting the line up by String.split(",") and tossing the result into a List, but it would be brittle and error-prone, so a better method of breaking up the components would be needed, or it just may be a better approach to use a CSV manipulation library which can properly handle the edge-cases.

心欲静而疯不止 2024-10-07 14:04:17

你有几个选择。

1. Organize your code like this:

  a. read line 1, 
  b. process and populate arraylist1, 
  c. read line 2,
  d. process and populate arraylist2,
  e. while readLine != null, 
     - process and populate arraylist3

2. Or like this:
  a. create a line counter `lineCnt`
  b. while readLine != null, 
     switch lineCnt++
     case 0
     - process and populate arraylist1
     case 1
     - process and populate arraylist2
     default
     - process and populate arraylist3

You have a couple options.

1. Organize your code like this:

  a. read line 1, 
  b. process and populate arraylist1, 
  c. read line 2,
  d. process and populate arraylist2,
  e. while readLine != null, 
     - process and populate arraylist3

2. Or like this:
  a. create a line counter `lineCnt`
  b. while readLine != null, 
     switch lineCnt++
     case 0
     - process and populate arraylist1
     case 1
     - process and populate arraylist2
     default
     - process and populate arraylist3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文