-1

I am getting the following error:

logcat output with the error

scan = new Scanner(new File("file:///android_lib/"+StationNM+".csv"));

I have 아현.csv in the lib folder, but android throws FileNotFoundException.

how can I solve this? adfasdfa

my source is

mport java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.Object;

public class model {


    public float modelPredict(String StationNM, String UPandDOWN, int day, int hour, int minute)
    {
 Scanner scan = null;
        try {
            scan = new Scanner(new File("file:///android_lib/"+StationNM+".csv"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ArrayList<String[]> records = new ArrayList<String[]>();
        ArrayList<Integer> model_list = new ArrayList<Integer>();

        while (scan.hasNext()) {
            String[] record;
            record = scan.nextLine().split(",");
            records.add(record);
        }
}

this issue was solved
GoBackess
  • 404
  • 3
  • 17

1 Answers1

1
scan = new Scanner(new File("file:///android_lib/"+StationNM+".csv"));

The File constructor takes filesystem paths. What you are passing:

  • Is not a filesystem path, as paths do not have schemes like file:// or http:// or content://

  • Refers to a non-existent directory on the filesystem

I have 아현.csv in the lib folder

Put it in src/main/assets/ of your module. Pass in an AssetManager to your modelPredict() method, and use open() on AssetManager to get an InputStream that you can pass to the Scanner constructor. You get an AssetManager by calling getAssets() on a Context, such as your Activity or Service.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @GoBackess: As I wrote, you get an `AssetManager` by calling `getAssets()` on a `Context`, such as your `Activity` or `Service`. – CommonsWare Jun 05 '19 at 13:57
  • i try `AssetManager assetManager = getApplication().getAssets();` but `java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.app.Application.getAssets()' on a null object reference` – GoBackess Jun 05 '19 at 14:19
  • @GoBackess: `getApplication()` is returning `null`. If you are trying this in a field initializer of your activity, you need to wait on calling methods like `getApplication()` until `onCreate()` or later. – CommonsWare Jun 05 '19 at 14:25
  • @GoBackess: A class named `model` is an odd one for an activity. Did you start `model` using `startActivity()`? – CommonsWare Jun 05 '19 at 14:35
  • No this is for calculate class – GoBackess Jun 05 '19 at 14:37
  • and Context doesn't have getAssets() – GoBackess Jun 05 '19 at 14:37
  • public float modelPredict(String StationNM, String UPandDOWN, int day, int hour, int minute) { AssetManager assetManager = (AssetManager) Context.getAssets(); occur `Non-static method 'getAssets()' cannot be referenced from a static context` – GoBackess Jun 05 '19 at 14:39
  • 1
    @GoBackess: "and Context doesn't have getAssets()" -- yes, [it does](https://developer.android.com/reference/android/content/Context#getAssets()). You are trying to call `getAssets()` on the class, not on an object that is an instance of a `Context`, such as your `Activity` or `Fragment`. – CommonsWare Jun 05 '19 at 14:43