简体   繁体   中英

Netbeans Java: Where to put my CSV file?

I followed a tutorial to create some simple code to output the contents of a csv file. However, I always get the following message:

java.io.FileNotFoundException: Data.csv (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.util.Scanner.<init>(Scanner.java:656)
at testing.csv.files.Test.main(Test.java:26)
BUILD SUCCESSFUL (total time: 0 seconds)

So I guess this means that the program is running, but it can't find my csv file. Basically, I just dragged and dropped it from my desktop into the "Source Packages" file in my Java Project, which is where my Test.java file is. I've also tried putting it in the "testing.csv.files", but that did not work either. Neither did putting it in the "Test Packages".

I've ran out of ideas. Where am I supposed to put this csv file?

here is my code:

package testing.csv.files;


 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;
 import java.util.logging.Level;
 import java.util.logging.Logger;


public class Test {

public static void main(String[] args) {
    //.csv comma separated values
    String fileName = "Data.csv";
    File file = new File(fileName); // TODO: read about File Names
    try {
        Scanner inputStream = new Scanner(file);
        while (inputStream.hasNext()){
            String data = inputStream.next();
            System.out.println(data);
        }
        inputStream.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

You could try pointing to the full path of the file.

For example: String fileName = "Desktop/Data.csv";

FYI - You can copy the full path of a file by right clicking on a file while holding shift, then selecting: "copy as path".

将您的csv文件(即Data.csv )放到您的项目文件夹中,然后它将正常工作,我尝试过您的代码,对我来说很好

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM