eclipse Java中csv文件的文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13670593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:32:18  来源:igfitidea点击:

File path for csv-file in Java

javaeclipsecsvfilereader

提问by Miss Marple

I have a project in java, in which I read in some csv-data. I have to type in the specific path. Now I would like to send this project to someone else, but he should not have to change the path information. He should be able to run the project, without changing anything in the code.

我有一个 Java 项目,我在其中读取了一些 csv 数据。我必须输入特定路径。现在我想把这个项目发给别人,但他应该不必更改路径信息。他应该能够运行该项目,而无需更改代码中的任何内容。

I have already put the csv-data into my source-file but I get an exception, when trying to use this path.

我已经将 csv 数据放入我的源文件中,但是在尝试使用此路径时出现异常。

BufferedReader in = new BufferedReader(new FileReader("text.csv"));

Exception while reading csv file: java.io.FileNotFoundException: text.csv (No such file or directory)

I am using as IDE eclipse.

我正在使用 IDE eclipse。

回答by Yogendra Singh

Put the file in root of your Java source folderand then make sure that its getting complied to your classes or bin or target folder(where all your compiled classes are going). Once done, then change your code as below:

将文件放在 Java 源文件夹的根目录中,然后确保它已编译到您的类或 bin 或目标文件夹(所有已编译的类所在的位置)。完成后,更改您的代码,如下所示:

   InputStream inputStream = 
                  getClass().getClassLoader().getResourceAsStream("text.csv");
   BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream ));

When you package your project for distribution, make sure your text.csvis packaged in the same folder i.e. root of classes folders.

当您打包项目以进行分发时,请确保您的项目text.csv打包在同一个文件夹中,即类文件夹的根目录。

回答by Phil K

I would pass the path of the .CSV file through the program arguments like so:

我会通过程序参数传递 .CSV 文件的路径,如下所示:

java MyCsvReader C:\Users\Me\file.csv


And then access the passed argument via argsin your mainmethod:


然后通过args您的main方法访问传递的参数:

public static void main(String[] args) {
    if(args.length != 1) {
        System.out.println("Correct usage: MyCsvReader <CSV path>");
        System.exit(1);
        return;
    }

    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    // ...
}

回答by Ivan

Depending on the project, just have the program prompt the user for a file path to the CSV file. If it is a command line program, use System.into read in a path string and use that in place of "text.csv".

根据项目的不同,只需让程序提示用户输入 CSV 文件的文件路径即可。如果是命令行程序,请使用System.in读取路径字符串并使用它代替"text.csv".

Something like:

就像是:

Scanner input = new Scanner( System.in );
String path = input.next();
BufferedReader in = new BufferedReader(new FileReader(path));

I also recommend making sure that a file actually exists at the path first though and kick back a friendly message instead of just throwing a raw exception.

我还建议首先确保文件确实存在于路径中,然后回退一条友好的消息,而不是仅仅抛出原始异常。