0

I am trying to read values from CSV file which is present in package com.example. But when i run code with the following syntax:

DataModel model = new FileDataModel(new File("Dataset.csv"));

It says:

java.io.FileNotFoundException:Dataset.csv

I have also tried using:

DataModel model = new FileDataModel(new File("/com/example/Dataset.csv"));

Still not working. Any help would be helpful. Thanks.

Smit
  • 4,685
  • 1
  • 24
  • 28
  • It's not a class - so it's not really in a package as such. Is it a file on the file system, or is it in a jar file? We don't really have enough information about where the file is in the context of how you're running the code. – Jon Skeet May 21 '15 at 21:12
  • `is present in package com.example` Use `getClass().getResource` or `getClass().getResourceAsStream` on the current class, passing the package and name of the resource (eg "/com/example/Dataset.csv"). – copeg May 21 '15 at 21:18

3 Answers3

0
public class ReadCVS {

  public static void main(String[] args) {

    ReadCVS obj = new ReadCVS();
    obj.run();

  }

  public void run() {

    String csvFile = "file path of csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // Do stuff here

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");
  }

}
Ritesh Karwa
  • 2,196
  • 1
  • 13
  • 17
0

CSV file which is present in package com.example

You can use getResource() or getResourceAsStream() to access the resource from within the package. For example

InputStream is = getClass().getResourceAsStream("/com/example/Dataset.csv");//uses absolute (package root) path
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//read from BufferedReader

(note exception handling and file closing are omitted above for brevity)

copeg
  • 8,290
  • 19
  • 28
0

If this is the FileDataModel from org.apache.mahout.cf.taste.impl.model.file then it can't take an input stream and needs just a file. The problem is you can't assume the file is available to you that easily (see answer to this question).

It might be better to read the contents of the file and save it to a temp file, then pass that temp file to FileDataModel.

InputStream initStream = getClass().getClasLoader().getResourceAsStream("Dataset.csv");
//simplistic approach is to put all the contents of the file stream into memory at once
//  but it would be smarter to buffer and do it in chunks
byte[] buffer = new byte[initStream.available()];
initStream.read(buffer);

//now save the file contents in memory to a temporary file on the disk
//choose your own temporary location - this one is typical for linux
String tempFilePath = "/tmp/Dataset.csv";  
File tempFile = new File(tempFilePath);
OutputStream outStream = new FileOutputStream(tempFile);
outStream.write(buffer);

DataModel model = new FileDataModel(new File(tempFilePath));
...
Community
  • 1
  • 1
Vic
  • 437
  • 3
  • 8
  • Hi Vic thanks so much. I was looking specifically for Mahout. Can you please let me know what should be the path for tempFilePath in Windows. – Fahad Alam May 21 '15 at 22:39
  • np. Essentially the temp location can be anything you want that your app will have read/write permissions to. But [this answer](http://stackoverflow.com/questions/16352326/when-does-system-getpropertyjava-io-tmpdir-return-c-temp) explains default temp locations very well. So for windows it is usually based on environment variable **TEMP** or java property `System.getProperty("java.io.tmpdir")` - but this can vary depending on many things. – Vic May 21 '15 at 22:52