2

I am trying to read data from .csv-Files in an Android Activity using java.util.Scanner, but no matter what I do, I always get FileNotFoundException.

This is the onCreate() - Method in my Activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Context ctx = getApplicationContext();
    Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
    setContentView(R.layout.activity_map);

    map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);
    IMapController mapController = map.getController();
    GeoPoint startPoint = new GeoPoint(35.63291, 139.88039);
    mapController.setCenter(startPoint);
    mapController.setZoom(18);

    Marker startMarker = new Marker(map);
    startMarker.setPosition(startPoint);
    startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(startMarker);

    List<GeoPoint> geoPoints = new ArrayList<>();

    points();

}

I am calling the static method points(), which uses the Scanner to read from .csv-Files.

This is how the method points() looks like:

 public static void points() {

 try {

    Scanner sc = new Scanner(new File("app/sampledata/nodes.csv"), "UTF-8");

    while (sc.hasNextLine()) {
        String[] content = sc.nextLine().split(";");
        content[6] = content[6].substring(2, content[6].length() - 2);
        String[] nodes = content[6].split(",");
        for (int i = 0; i < nodes.length; i++) {

            try {
                Scanner scanner = new Scanner(new File("app/sampledata/nodes.csv"), "UTF-8");
                while (scanner.hasNextLine()) {
                    String[] nodesContent = sc.nextLine().split(";");
                    String node = nodesContent[0];
                    if (nodes[i].equals(node)) {
                        String latitude = nodesContent[6];
                        String longitude = nodesContent[7];
                        // latLon.add(latitude + "; " + longitude);
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
    }
} catch (FileNotFoundException e) {
    System.exit(1);
} }

The paths should be correct since the same code is working executed as .java-Class. But when I start the application as an Android application it always throws FileNotFoundException when reaching the line with the Scanner. What could be the reason for that?

ladyeli555
  • 59
  • 6

0 Answers0