【问题标题】:java.io.FileNotFoundException in OpencsvOpencsv 中的 java.io.FileNotFoundException
【发布时间】:2019-05-26 14:01:14
【问题描述】:

我想读取一个 CSV 文件。我试过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();
        }

但我得到一个例外:

/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)

我该如何解决?

我不知道“csvfile.getAbsolutePath()”中输入了什么

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

【问题讨论】:

    标签: android


    【解决方案1】:

    替换:

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

    与:

    CSVReader reader = new CSVReader(new FileReader(csvfile));
    

    【讨论】:

    • W/System.err: java.io.FileNotFoundException: /storage/emulated/0/아현.csv(没有这样的文件或目录)T.T
    • @GoBackess:也许你没有同名的文件。或者,也许您没有在清单和运行时请求READ_EXTERNAL_STORAGE 权限。
    猜你喜欢
    • 2014-05-23
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多