I am currently trying to get a list of files in a directory. When I try to get the filehandle for the directory it says that it is a file, but it isn't. When I then readString()
from it, it returns the filenames of the files in it. This only happens in the IDE (Eclipse NEON.2), but when I export to JAR it crashes when I try to run.
How can I get LibGDX to recognise it as a directory and get all files in that directory. One last thing I have seen many anwers saying that list()
doesn't work on desktop, but I have also read that it works. Can someone check if this is true or not.
public void loadEnemyBaseTypes(){
ArrayList<FileHandle> enemyBaseFiles = new ArrayList<FileHandle>();
FileHandle enemyBaseDirectory =
Gdx.files.internal("prototypes/enemybases");
System.out.println(enemyBaseDirectory.exists());
System.out.println(enemyBaseDirectory.isDirectory());
String[] fileNames = enemyBaseDirectory.readString().split("\n");
for(int i = 0; i < fileNames.length; i++)
System.out.println(fileNames[i]);
}
IDE Output :
true
false
base_0.enybse
base_1.enybse
base_2.enybse
base_3.enybse
base_4.enybse
base_5.enybse
Terminal Output :
true
false
Exception in thread "LWJGL Application" java.lang.NullPointerException
at java.io.FilterInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.Reader.read(Unknown Source)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:207)
at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:191)
at dev.thomaslienbacher.simplegame.controllers.EnemyManager.loadEnemyBaseTypes(EnemyManager.java:53)
at dev.thomaslienbacher.simplegame.controllers.EnemyManager.loadAssets(EnemyManager.java:33)
at dev.thomaslienbacher.simplegame.scene.GameScene.loadAssets(GameScene.java:62)
at dev.thomaslienbacher.simplegame.Game.update(Game.java:149)
at dev.thomaslienbacher.simplegame.Game.render(Game.java:134)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
I created nested folder inside Assets list -> enemy
then I keep three .png
files inside enemy folder having name 1.png
, 2.png
and 3.png
.
FileHandle enemyBaseDirectory = Gdx.files.internal("list/enemy");
System.out.println(enemyBaseDirectory.exists());
System.out.println(enemyBaseDirectory.isDirectory());
FileHandle allList[]=enemyBaseDirectory.list();
for (FileHandle fileHandle:allList)
System.out.println(fileHandle);
Output on IDE Run
tab :
true
true
list/enemy/1.png
list/enemy/2.png
list/enemy/3.png
There is no any output on IDE local Terminal
. My IDE is Intellij IDEA
, hopefully result should be same on other IDE.
That is my expected output.
FileHandle's readString()
reads the entire file into a string using the platform's default charset and if file handle represents a directory, doesn't exist, or could not be read then throw GdxRuntimeException
.
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.