1

my code is

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));
        //            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();
    }
}

and error is

java.io.FileNotFoundException: /storage/emulated/0/이대.csv (No such file or directory)

my .java file path is

C:\Users\Mir\Python\capstone\SC\MySubwayProject\app\src\main\java\com\example\mysubwayproject

and .csv file path is

C:\Users\Mir\Python\capstone\SC\MySubwayProject\app\src\main\assets\아현.csv

AndroidManifest.xml is (added user-permission)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mysubwayproject">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Main2Activity"
            android:label="@string/title_activity_main2"
            android:theme="@style/AppTheme.NoActionBar"></activity>
        <activity android:name=".TimeSetter" />
        <activity android:name=".ResultView" />
        <activity android:name=".StartStation" />
        <activity android:name=".EndStation" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

how can I import .csv files in assets path?

and my Repository is https://github.com/Lay4U/CapstOne/tree/master/SC/MySubwayProject

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
GoBackess
  • 404
  • 3
  • 17
  • What are you trying to do here ? You can not modify Assets at Runtime .. – ADM May 27 '19 at 04:38
  • i just want read csv file so tried https://stackoverflow.com/questions/43055661/reading-csv-file-in-android-app – GoBackess May 27 '19 at 04:48
  • You have stored the csv file in the app assets. The external directory does not contain the assets. Read this how to load assets: https://stackoverflow.com/questions/22706535/android-asset-loading – Robert May 27 '19 at 08:03

1 Answers1

1

I have updated your method. USe it in your code

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");
        File dir = Environment.getExternalStorageDirectory();
        File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
        CSVReader reader = new CSVReader(new FileReader(yourFile));
        //            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();
    }
}
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Google
  • 2,183
  • 3
  • 27
  • 48
  • java.io.FileNotFoundException: /storage/emulated/0/path/to/the/file/inside/the/sdcard.ext (No such file or directory) T.T – GoBackess May 27 '19 at 05:09