0

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.

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

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.

Miss Marple
  • 149
  • 2
  • 4
  • 12
  • 2
    If the file isn't there, it isn't there. Consider packaging the csv file in the jar as a resource, if the other person doesn't need to use their own data. – Dave Newton Dec 02 '12 at 15:18
  • And consider `Class.getResource()` – nullpotent Dec 02 '12 at 15:19
  • The approach using FileReader will assume that the file exists in the current working directory. You can either make sure the file really is in the current working directory; or, package it as Dave Newton mentions; or make sure the file is somewhere on the classpath and use getResourceAsStream instead. This last approach is the recommended way if the program in question happens to be a webapp, see http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream. – icey502 Dec 02 '12 at 17:29

3 Answers3

1

Put the file in root of your Java source folder and 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:

   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.csv is packaged in the same folder i.e. root of classes folders.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

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

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


And then access the passed argument via args in your main method:

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]));
    // ...
}
Phil K
  • 4,939
  • 6
  • 31
  • 56
-1

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.in to read in a path string and use that in place of "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.

Ivan
  • 10,052
  • 12
  • 47
  • 78