Home > Back-end >  FileReader can't open the txt file
FileReader can't open the txt file

Time:12-21

I know this is a subject that has been done over and over but I can't solve my issue with all the solutions I tried so here am I :

FileHandle file = Gdx.files.internal("map.txt");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file.path()));
        String s = "";
        int count = 0;
        while((s = bufferedReader.readLine()) != null) {
            System.out.println(s);
            map[count] = s.split(" ");
            count  ;
        }
        bufferedReader.close();

and my stack trace :

java.io.FileNotFoundException: map.txt (Le fichier spécifié est introuvable)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:108)
    at java.base/java.io.FileReader.<init>(FileReader.java:60)
    at com.simpower.Tilemap.fillMap(Tilemap.java:51)
    at com.simpower.Tilemap.<init>(Tilemap.java:32)
    at com.simpower.GameScreen.<init>(GameScreen.java:19)
    at com.simpower.SimPower.create(SimPower.java:14)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:150)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:127)

In english the first line should be something like java.io.FileNotFoundException: map.txt (specified file cannot be found)

The file is in an asset folder and its path is /SimPower-core/assets/map.txt

I am using libgdx.

I tried giving the full path to the FileReader to remove the .txt change my file location but none of it solved my issue.

Help is much appreciated.

CodePudding user response:

Can you change the path of the file and put it on the root for example on the desktop ?

Then replace the file.path by your new path.

Something like this BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\YourPath"));

CodePudding user response:

Okay I finally found the answer to my problem in only forget the / in front of the file's name... The right line is : FileHandle file = Gdx.files.internal("/map.txt");

  • Related