0

This is a function of my Android Studio Project.
I need the right String to put in the brackets in "new FileReader("stringpath")" everything I tried doesn't work.

That's my code:

private ArrayList<Spieler> generiereSpieler(int anzahl){
        ArrayList<Spieler> spielerListe = new ArrayList<>();
        ArrayList<String> spielerListeString = new ArrayList<>();

        BufferedReader file = null;

        try{
            file = new BufferedReader(new FileReader("spieler.csv"));

            while(file.ready()){
                spielerListeString.add(file.readLine());
            }
        } catch(NullPointerException | IOException e){
            System.out.println("FEHLER: Konnte CSV-Datei (Spieler) nicht einlesen");
        } finally {
            try{
                file.close();
            } catch(NullPointerException | IOException e){
                System.out.println("FEHLER: Konnte CSV-Datei (Spieler) nicht schließen");
            }
        }

        try{
            StringTokenizer stringTokenizer = null;
            Random random = new Random();
            int spielerAnzahl = spielerListeString.size();
            int spielerIndex = 1; //TODO
            Spieler spieler;
            for(int index = 0; index < anzahl; index++){
                spielerIndex = random.nextInt(spielerAnzahl);
                stringTokenizer = new StringTokenizer(spielerListeString.get(spielerIndex), ",");
                spieler = new Spieler(stringTokenizer.nextToken(), stringTokenizer.nextToken(), null);
                spielerListe.add(spieler);
            }
        } catch(NullPointerException | NoSuchElementException e){
            System.out.println("FEHLER: Konnte Liste (Spieler) nicht verarbeiten");
        } catch (IllegalArgumentException e){
            System.out.println("FEHLER: IllegalArgumentException");
            e.printStackTrace();
        }

        return spielerListe;
    }

The file is never beeing found? What do I need to put in there?
Do I need to put the File in the assets or raw folder and how can I get its path?

  • It's depends of you file location. For test purpose you can use full path like `c:\test\test_file.txt` for example (for windows), if it's work you should investigate how to find files from java... – borino Sep 17 '20 at 13:27

1 Answers1

0

Try placing your "spieler.csv" file along with source folder, inside the project folder.