简体   繁体   English

Java FileInputStream找不到文件(LibGDX)

[英]Java FileInputStream can't find file (LibGDX)

So I have this class: 所以我有这个课:

http://pastebin.com/EwXFwuZz http://pastebin.com/EwXFwuZz

And this directory tree: http://s14.directupload.net/file/d/3099/uskko5mo_png.htm 以及这个目录树: http : //s14.directupload.net/file/d/3099/uskko5mo_png.htm

And I'm working with the LibGDX Framework on this project. 我正在与此项目一起使用LibGDX框架。 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. 不幸的是,LibGDX的内置FileHandling系统不支持逐行读取,因此我认为应该坚持使用库存Java。

However I'm getting this "FileNotFound" Exception: 但是我收到此“ FileNotFound”异常:

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. 使用LibGDX,可以找到该文件,但将其读入一个单一的巨大字符串。 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". 对于不熟悉LibGDX的人,我正在上层的“ CuteGameXVII”项目中进行编码,但为了进行编译,我在“ CuteGameXVII-desktop”中运行了“ Main”。 The assets folders are linked via eclipse and worked for other resources so far. 资产文件夹是通过eclipse链接的,迄今为止可以用于其他资源。

Am I missing something super obvious here? 我在这里想念什么超级明显吗? If so, please help me :P Thank you in advance. 如果是这样,请帮助我:P提前谢谢。

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 . 如果您从路径$DIR的目录运行Java程序,则输入文件应位于$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: 为了使用LibDGX从资产目录中读取文件,您必须使用LibGDX方法替换:

new FileInputStream("SomeFile.txt")

by 通过

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

assuming "someFile.txt" is in your asset root folder. 假设“ someFile.txt”位于您的资产根文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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