简体   繁体   中英

Java FileInputStream can't find file (LibGDX)

So I have this class:

http://pastebin.com/EwXFwuZz

And this directory tree: http://s14.directupload.net/file/d/3099/uskko5mo_png.htm

And I'm working with the LibGDX Framework on this project. This is basically my problem:

I have a file that contains level information in "chunks". Each line is one chunk. I want to read the file line per line. Unfortunately the built in FileHandling system of LibGDX doesn't support line by line reading so I thought to stick to the stock java one.

However I'm getting this "FileNotFound" Exception:

java.io.FileNotFoundException: ./assets/data/lvls/example.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.io.FileReader.<init>(FileReader.java:55)
    at gemini.cute.game.xvii.database.LevelReader.<init>(LevelReader.java:49)
    at gemini.cute.game.xvii.core.MainLauncher.create(MainLauncher.java:40)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:124)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:107)

With LibGDX the file is found but read into ONE single gigantic string. And for some reason with the same path (even going from the root) the file isn't found.

For people not familiar with LibGDX I'm coding in the upper "CuteGameXVII" project but for compilation I run the "Main" in "CuteGameXVII-desktop". The assets folders are linked via eclipse and worked for other resources so far.

Am I missing something super obvious here? If so, please help me :P Thank you in advance.

If you're running the Java program from a directory with path $DIR , the input file should be at $DIR/assets/data/lvls/example.txt . Based on the exception that you've received, the input file doesn't exist at this location.

I'd suggest that you first try using the absolute path to the input file in your code. Then, figure out what the relative path to it is.

I experienced this issue too. In order to read a file from your asset directory with LibDGX you must use the LibGDX method replacing:

new FileInputStream("SomeFile.txt")

by

Gdx.files.internal("SomeFile.txt").read()

assuming "someFile.txt" is in your asset root folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM