0

I want read a CSV file. I tried Reading CSV File In Android App

public float modelPredict(String StationNM, int day, int hour, int minute) {
        int[] model = new int[0];
        try {
            File csvfile = new File(Environment.getExternalStorageDirectory() + "/"+StationNM+".csv");
            CSVReader reader = new CSVReader(new FileReader("csvfile.getAbsolutePath()"));
//            CSVReader reader = new CSVReader(new FileReader(StationNM+".csv"));
            String[] nextLine;
            while ((nextLine = reader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                System.out.println(nextLine[0] + nextLine[1] + "etc...");
            }
            model = new int[nextLine.length];
            for (int i=0; i<nextLine.length; i++) {
                model[i] = Integer.parseInt(nextLine[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(this, "The specified file was not found", T oast.LENGTH_SHORT).show();
        }

But I get an exception:

/System.err: java.io.FileNotFoundException: csvfile.getAbsolutePath() (No such file or directory)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:231)
        at java.io.FileInputStream.<init>(FileInputStream.java:165)

How can I solve it?

I don't know what is input in "csvfile.getAbsolutePath()"

In my repository is https://github.com/Lay4U/CapstOne/tree/master/SC/MySubwayProject

Zoe
  • 27,060
  • 21
  • 118
  • 148
GoBackess
  • 404
  • 3
  • 17

1 Answers1

1

Replace:

CSVReader reader = new CSVReader(new FileReader("csvfile.getAbsolutePath()"));

with:

CSVReader reader = new CSVReader(new FileReader(csvfile));
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • W/System.err: java.io.FileNotFoundException: /storage/emulated/0/아현.csv (No such file or directory) T.T – GoBackess May 26 '19 at 14:15
  • @GoBackess: Perhaps you do not have a file with that name. Or, perhaps you did not request the `READ_EXTERNAL_STORAGE` permission in the manifest and at runtime. – CommonsWare May 26 '19 at 14:19