java.io.FileNotFoundException,系统找不到指定的路径(cvs文件)java

标签 java csv path

一切似乎都井然有序,但不知怎的却并非如此。 这是代码;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

     String csvFilename = "src/example.csv"; 
            CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
            String[] row = null;
            String total = "";
            while((row = csvReader.readNext()) != null) {

                for( int i = 0 ; i < 200 ; i++ ){  // no higher than num of columns to be found or error

                    String saveAway = row[i];
                    //tabl[saveInThisRow][i] = row[i];
                    tabl[saveInThisRow][i] = saveAway.replace('_', ' ');

                }
                saveInThisRow++;
                if(saveInThisRow == 50) { saveInThisRow = 0; break; }

            }
            //saveInThisRow = 0;
            // ctrl-i  = auto format

            csvReader.close();

据我所知,路径是正确的(在 src 中),也许 csv 有问题? 它在 Eclipse 中工作得很好,但现在在 Intellij 中它坏了......这里发生了什么?

这是堆栈跟踪;

java.io.FileNotFoundException: src\example.csv (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at net.klingt.example.LoadCsv.readCsv(LoadCsv.java:61)
    at net.klingt.example.ProcessingExample.draw(ProcessingExample.java:252)
    at processing.core.PApplet.handleDraw(PApplet.java:2386)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Thread.java:745)

现在我有了这个;

 public void readCsv(   ) throws IOException, URISyntaxException { // throws IOException  ---  String[] args
        System.out.println(".............");
        System.out.println(System.getProperty("user.dir"));

        val = 20; // testing purposes
        String [][] tab  = new String [100][400];
        int saveInThisRow = 0;

        File file = new File(getClass().getResource("src/resources/GEMSTONES05.csv").toURI());
        String csvFilename = "src/resources/GEMSTONES05.csv";

        CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
        String[] row = null;
        String total = "";
        while((row = csvReader.readNext()) != null) {

            for( int i = 0 ; i < 20 ; i++ ){  // no higher than num of columns to be found or error
                String saveAway = row[i];
                parent.println("CVS read  " + saveAway);
                //tabl[saveInThisRow][i] = row[i];
                tabl[saveInThisRow][i] = saveAway.replace('_', ' ');
            }
            saveInThisRow++;
            if(saveInThisRow == 20) { saveInThisRow = 0; break; }

        }

        csvReader.close();

        for( int i = 0 ; i < 300 ; i++){

        }

    }

我有点迷失如何以及在哪里使用"file",这是堆栈跟踪

C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.0.3\jre\jre\bin
Exception in thread "Animation Thread" java.lang.NullPointerException
    at net.klingt.example.LoadCsv.readCsv(LoadCsv.java:41)
    at net.klingt.example.ProcessingExample.draw(ProcessingExample.java:253)
    at processing.core.PApplet.handleDraw(PApplet.java:2386)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Thread.java:745)

我不再有文件未找到错误,而是现在出现空指针异常......

最佳答案

试试这个

File file = new File(getClass().getResource("/example.csv").toURI());

它从项目的 src 文件夹中获取文件

并确保 example.csv 存在于 src 文件夹中。

将 readCSV() 方法更改为以下内容..

public void readCsv(   ) throws IOException, URISyntaxException { // throws IOException  ---  String[] args
    System.out.println(".............");
    System.out.println(System.getProperty("user.dir"));

    val = 20; // testing purposes
    String [][] tab  = new String [100][400];
    int saveInThisRow = 0;

    File file = new File(getClass().getResource("/resources/GEMSTONES05.csv").toURI());
    //String csvFilename = "/resources/GEMSTONES05.csv";

    CSVReader csvReader = new CSVReader(new FileReader(file));
    String[] row = null;
    String total = "";
    while((row = csvReader.readNext()) != null) {

        for( int i = 0 ; i < 20 ; i++ ){  // no higher than num of columns to be found or error
            String saveAway = row[i];
            parent.println("CVS read  " + saveAway);
            //tabl[saveInThisRow][i] = row[i];
            tabl[saveInThisRow][i] = saveAway.replace('_', ' ');
        }
        saveInThisRow++;
        if(saveInThisRow == 20) { saveInThisRow = 0; break; }

    }

    csvReader.close();

    for( int i = 0 ; i < 300 ; i++){

    }

}

在获取资源时,不必写src文件夹名称。因为在运行时,jvm会从src文件夹中获取资源并寻找下一条路。

关于java.io.FileNotFoundException,系统找不到指定的路径(cvs文件)java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30275143/

相关文章:

python - 排序csv python 2.7

grails - 在 Grails 插件中访问资源

java - 属性文件的相对路径

javascript - Heroku:NodeJS Express 服务器无法提供图像

java - 使用二分查找插入 ArrayList<Occurrence>

java - 通过 Windows 拖放启动 Java 类

java - Hi-Lo Id 发生器的改进

php - 将数据导出到 CSV 时处理一对多关系

linux - 有没有办法在 Unix 中删除文件中的重复 header ?

java - Spring 工具套件4 : not possible to open projects after the update to version 4. 5.0