读取.csv文件(系统找不到指定的文件) [英] Reading a .csv file (The system cannot find the file specified)

查看:979
本文介绍了读取.csv文件(系统找不到指定的文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了这个问题和类似的问题,但我的问题是它刚才工作正常。我哪里做错了?
这是我用来解决如何读取.csv文件并将其中的值传递给其他文件的一个小例子,然后在更大的应用程序中实现它。

I see this question and similar questions already but my problem is that it was working fine until a moment ago. Where did I go wrong? This is a small example I made to work out how to read a .csv file and to pass the values in it to something else, before implementing it in a larger application.

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

    public class Main {


public static void main(String[] args) {

    String fileName = "data.csv";
    File file = new File(fileName);

    try {
        Scanner inputStream = new Scanner(file);
        inputStream.next();//Ignore first line
        while(inputStream.hasNext()) {
            String data = inputStream.next();//gets a whole line
            //create a string array where value at 0 is the name, value at 1 is the quantity, and value at 2 is the price
            String[] values = data.split(",");
            int quantity = Integer.parseInt(values[1]);
            System.out.println(values[0] + "\n" + quantity + "\n" + values[2]);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}

我为我的大型计划做了这个它给了我页面找不到错误,当我去仔细检查这个项目时,它也不会运行。它给了我同样的错误。我重新启动了Eclipse并检查了几次以查找我是否意外地遗漏了某些内容或更改了某些内容。 data.csv 就在我的src文件夹中。我甚至喋喋不休地让它出现在Eclipse项目视图中。我缺少什么?

I went to do this for my larger program and it gave me the page not found error and when I went to double check this project, it would not run either. It gives me the same error. I restarted Eclipse and checked a few times to find if I accidentally left something out or changed something. data.csv is right there in my src folder. I even dagged it over to make it show up in the Eclipse project view. What am I missing?

名称,数量,价格
茶,100,2.50
可乐,50,2.50
Lays,32,1.00
Cheetos,34,1.10
士力架,30,1.25
Butterfinger,70,1.50
五,40,0.75
轨道, 50,0.90

推荐答案

这仅限于使用opencsv允许的最简单,最强大的方法你可以开始运行了。

This is limited to the easiest, most powerful way of using opencsv to allow you to hit the ground running.

为了阅读,创建一个bean来包含你想要读取的信息,用opencsv注释注释bean字段,然后执行以下操作:

For reading, create a bean to harbor the information you want to read, annotate the bean fields with the opencsv annotations, then do this:

List<MyBean> beans = new CsvToBeanBuilder(FileReader("yourfile.csv"))
   .withType(Visitors.class).build().parse();

为了编写,创建一个bean来包含你想要写的信息,用bean注释bean字段opencsv annotations,然后执行以下操作:

For writing, create a bean to harbor the information you want to write, annotate the bean fields with the opencsv annotations, then do this:

// List<MyBean> beans comes from somewhere earlier in your code.
 Writer writer = new FileWriter("yourfile.csv");
 StatefulBeanToCsvBuilder beanToCsv = StatefulBeanToCsvBuilder(writer).build();
 beanToCsv.write(beans);
 writer.close();

假设您正在为分隔符使用选项卡,您可以执行以下操作:

Say you're using a tab for your separator, you can do something like this:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');

或带注释的阅读:

CsvToBean csvToBean = CsvToBeanBuilder(new FileReader("yourfile.csv"))
       .withSeparator('\t').build();

如果您单独引用转义的字符而不是双引号,则可以使用三个-argument构造函数:

And if you single-quoted your escaped characters rather than double-quoting them, you can use the three-argument constructor:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');

或带注释的阅读:

CsvToBean csvToBean = CsvToBeanBuilder(new FileReader("yourfile.csv"))
       .withSeparator('\t').withQuoteChar('\'').build();

最基本的,你可以使用opencsv解析输入并返回一个String [],这样:

At the most basic, you can use opencsv to parse an input and return a String[], thus:

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
     String [] nextLine;
     while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + nextLine[1] + "etc...");
     }

这篇关于读取.csv文件(系统找不到指定的文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆