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

java.io.FileNotFoundException,系统找不到java指定的路径(cvs文件),java,csv,path,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 FileRead

一切似乎都井井有条,但不知何故却并非如此。 这是代码

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();
我不再有文件未找到错误,而是现在它是一个空指针异常…

尝试使用此

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

    }

}
public void readCsv()抛出IOException,URISyntaxException{//抛出IOException---String[]参数
系统输出打印项次(“…………”);
System.out.println(System.getProperty(“user.dir”);
val=20;//测试目的
字符串[][]选项卡=新字符串[100][400];
int saveInThisRow=0;
File File=新文件(getClass().getResource(“/resources/GEMSTONES05.csv”).toURI());
//字符串csvFilename=“/resources/GEMSTONES05.csv”;
CSVReader CSVReader=新CSVReader(新文件读取器(文件));
字符串[]行=null;
字符串总数=”;
while((row=csvReader.readNext())!=null){
对于(int i=0;i<20;i++){//不大于要查找的列数或错误
字符串saveAway=行[i];
println(“CVS读取”+saveAway);
//tabl[saveInThisRow][i]=第[i]行;
tabl[saveInThisRow][i]=saveAway.replace(“”,“”);
}
saveInThisRow++;
如果(saveInThisRow==20){saveInThisRow=0;break;}
}
csvReader.close();
对于(int i=0;i<300;i++){
}
}
获取资源时,您不必编写
src
文件夹名。。因为在运行时,jvm将从
src
文件夹中获取资源,并查找下一个路径。

试试这个

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

    }

}
public void readCsv()抛出IOException,URISyntaxException{//抛出IOException---String[]参数
系统输出打印项次(“…………”);
System.out.println(System.getProperty(“user.dir”);
val=20;//测试目的
字符串[][]选项卡=新字符串[100][400];
int saveInThisRow=0;
File File=新文件(getClass().getResource(“/resources/GEMSTONES05.csv”).toURI());
//字符串csvFilename=“/resources/GEMSTONES05.csv”;
CSVReader CSVReader=新CSVReader(新文件读取器(文件));
字符串[]行=null;
字符串总数=”;
while((row=csvReader.readNext())!=null){
对于(int i=0;i<20;i++){//不大于要查找的列数或错误
字符串saveAway=行[i];
println(“CVS读取”+saveAway);
//tabl[saveInThisRow][i]=第[i]行;
tabl[saveInThisRow][i]=saveAway.replace(“”,“”);
}
saveInThisRow++;
如果(saveInThisRow==20){saveInThisRow=0;break;}
}
csvReader.close();
对于(int i=0;i<300;i++){
}
}
获取资源时,您不必编写
src
文件夹名。。因为在运行时,jvm将从
src
文件夹获取资源并查找下一个路径。

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

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

是否检查了当前工作目录?我不太了解eclipse,但可能eclipse在与intellij不同的工作目录下运行应用程序。当我右键单击cvs并选择file_path时,它显示它位于src中,因此应该是正确的是(?)这是两个不同的东西。可以使用选项-Duser.dir='/some/path/to/anywhere'调用应用程序(取决于IDE或maven的配置)。然后“new FileReader(csvFilename)”尝试打开文件“/some/path/to/anywhere/src/example.csv”。您能在问题中发布准确的堆栈跟踪吗?我添加了堆栈跟踪作为回答您是否检查了当前的工作目录?我不太了解eclipse,但可能eclipse在与intellij不同的工作目录下运行应用程序。当我右键单击cvs并选择file_path时,它显示它位于src中,因此应该是正确的是(?)这是两个不同的东西。可以使用选项-Duser.dir='/some/path/to/anywhere'调用应用程序(取决于IDE或maven的配置)。然后“new FileReader(csvFilename)”尝试打开文件“/some/path/to/anywhere/src/example.csv”。你能在你的问题中发布准确的堆栈跟踪吗?我添加了堆栈跟踪作为回答,但现在我得到了;线程“Animation thread”java.lang.NullPointerException中出现异常,更改变量为
File File=new File(getClass().getResource(“src/resources/GEMSTONES05.csv”).toURI())
to
File File File=new文件(getClass().getResource(“/resources/GEMSTONES05.csv”).toURI())
并在
CSVReader
中将
FileReader
对象作为
newfilereader(file)
传递。thx用于回复,但现在我得到了;线程“Animation thread”java.lang.NullPointerException中出现异常,更改变量为
File File=new File(getClass().getResource(“src/resources/GEMSTONES05.csv”).toURI())
文件文件=新文件(getClass().getResource(“/Resource
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++){

    }

}