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

时间:2015-05-16 11:34:55

标签: 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++){

        }

    }

我有点失落&amp;在哪里使用“文件”,这里是堆栈跟踪

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)

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

2 个答案:

答案 0 :(得分:3)

试试这个

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文件夹中获取资源并查找下一个路径。

答案 1 :(得分:0)

&#34; SRC / example.csv&#34;是一个相对路径,尝试绝对路径,如c:/work/src/example.csv。 FileNotFoundException表示无法找到文件,通常在您滥用相对路径时发生。您可以打印System.getProperty("user.dir")以查看进程的当前目录,然后使用正确的相对路径。