java.io.FileNotFoundException,系统找不到指定的路径(cvs文件)java [英] java.io.FileNotFoundException, The system cannot find the path specified (cvs file) java

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

问题描述

似乎都是为了,但不知何故不是。
这是代码;

All seems to be in order, but somehow it isn't. Here is the code;

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它的破碎...这是什么?

The path is as far as I can tell correct (its in the src), maybe there is is something wrong with the csv instead? It worked nicely in Eclipse, but now in Intellij its broken... whats going on here?

这是堆栈跟踪;

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++){

        }

    }

我有点失落了如何&在哪里使用文件,这里是堆栈跟踪

I am a bit lost how & where to use "file", here is the stack trace

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)

我不再有文件未找到错误,现在它的一个空指针异常....


I no longer have the file not found error, instead now its an null pointer exception....

推荐答案

尝试

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

它从 src 文件夹您的项目

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

and make sure that example.csv is present in src folder.

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

change you readCSV() method to the following..

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

while getting the Resource, you don't have to write the src folder name.. Because at runtime, jvm will get the resources from src folder and look for the next path.

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

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