我的代码是

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


错误是

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


我的.java文件路径是

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

和.csv文件路径是

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

AndroidManifest.xml是(添加了用户权限)

<?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>


如何在资产路径中导入.csv文件?

我的存储库是
https://github.com/Lay4U/CapstOne/tree/master/SC/MySubwayProject

最佳答案

我已经更新了您的方法。在您的代码中使用它


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

10-06 14:59