学习使用Java编写CSV Reader程序

问题描述 投票:-4回答:1

我正在尝试编写一个Java CSV阅读器程序(使用我迄今为止学到的东西,我是一个新手)。但是,我现在不知道我的代码有什么问题。数据表很简单:

 my,name,is,Tan
 are,you,okay
 i,am,okay,—

输出基本上没有显示任何东西。但是,当我删除它们时,它起作用了。以下是我的代码:

    public static void main(String[] args) throws IOException{
    Scanner read = new Scanner(new File("List3.csv"));
    while(read.hasNext()){//if the txt file has next line then continue
        String line = read.nextLine();
        String[] lineArray = line.split(",");
        //
        for(int i = 0; i < lineArray.length; i++){
                System.out.print(lineArray[i] + " ");
            }
        }
    }

非常感谢。

java csv
1个回答
0
投票

@Tan,下面完美适合我。确保您的csv没有损坏并且位于正确的位置,并且还提到了如下所示的完整路径。

package com.java;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Test {
 public static void main(String[] args) throws IOException {

    Scanner read = new Scanner(new File("C:\\Users\\abcdef\\Desktop\\list.csv"));

    while (read.hasNext()) {// if the txt file has next line then continue

        String line = read.nextLine();

        String[] lineArray = line.split(",");

        for (int i = 0; i < lineArray.length; i++) {
            System.out.print(lineArray[i] + " ");
        }
    }

    read.close();
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.