无法读取csv文件

时间:2016-02-22 11:11:06

标签: java csv

我在csv文件中写入数据但是当我在分割行中读取它时它会 给出空白输出

BufferedReader br = new BufferedReader(new FileReader("D://codework/bug/myProject/preprocess2.csv"));
BufferedReader resolved = new BufferedReader(new FileReader("D://codework/bug/myProject/result.csv"));
File file = new File("D://codework/bug/myProject/categorized1.csv");

FileWriter fileWriter = new FileWriter(file);

String line = "";
List l = new ArrayList();
List list = new ArrayList<>();
try {
    int i = 01;
    int j = 1;
    String ybest = "Str";
    while ((line = br.readLine()) != null) 
    {
        list.add(line);
        //System.out.println(line);
    }
    for (int l2 = 0; l2 < list.size(); l2++) {
        String str[] = ((String)list.get(l2)).split(",");
        String s = str[1];
        System.out.println(str[1]);
    }

1 个答案:

答案 0 :(得分:1)

如果您正在阅读文件中的整行,那么您应该使用itr.nextLine()代替itr.next()

更正的代码段:

while (itr.hasNext()) {
    String[] object = ((String) itr.nextLine()).split(",");
    System.out.println(object[0]);
    System.out.println(object[1]);
    System.out.println(object[2]);
}

输入:

foo bar 1,foo bar 2,foo bar 3

输出:

foo bar 1
foo bar 2
foo bar 3