Gdx.files.internal(...)包装器无法正常工作

时间:2014-11-25 02:17:13

标签: java file libgdx bufferedreader

我制作了一个包装ConfigurationFile课来帮助处理Gdx.files这些东西,它很长一段时间都运行良好,但现在它没有用,我也不知道这是为什么。

我有以下两种方法中的两种:internal(...)local(...)。两者之间的唯一区别是处理来自(File folder, String name)(String path)的参数的负载。

- 现在提供不必要的信息 -

<小时/> 的更新
经过更多配置后,我发现他们的行为并不相同。我有一个assets/files/文件夹Gdx.files.internal(...)可以访问正常,但ConfigurationFile.internal(...)将访问files/,并且他们的设置方式相同。我将为您提供用于测试的两段代码。

直接使用Gdx.files.internal(...)(按预期工作):

FileHandle handle = Gdx.files.internal("files/virus_data");
BufferedReader reader = null;
try {
    reader = new BufferedReader(handle.reader());
    String c = "";
    while ((c = reader.readLine()) != null) {
        System.out.println(c); // prints out all 5 lines on the file.
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (reader != null) reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

使用ConfigurationFile.internal(...)

// First part, calls ConfigurationFile#internal(String path)
ConfigurationFile config = ConfigurationFile.internal("files/virus_data");

// ConfigurationFile#internal(String path)
public static ConfigurationFile internal(String path) {
    ConfigurationFile config = new ConfigurationFile();
    // This is literally calling Gdx.files.internal("files/virus_data");
    config.handle = Gdx.files.internal(path);
    config.file = config.handle.file();
    config.folder = config.file.getParentFile();
    config.init();
    return config;
}

// ConfigurationFile#init()
protected void init() {
    // File not found.
    // Creates a new folder as a sibling of "assets"
    // Creates a new file called "virus_data"
    if (!folder.exists()) folder.mkdirs();
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else loadFile();
}

// ConfigurationFile#loadFile()
protected void loadFile() {
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(handle.reader());
        String c = "";
        while ((c = reader.readLine()) != null) {
            System.out.println(c);
            if (!c.contains(":")) continue;
            String[] values = c.split(":");
            String key = values[0];
            String value = values[1];
            if (values.length > 2) {
                for (int i = 2; i < values.length; i++) {
                    value += ":" + values[i];
                }
            }
            key = key.trim();
            value = value.trim();
            mapValues.put(key, value);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null) reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我无法理解的是这两种方式之间的区别在于它导致我的ConfigurationFile在{{1}的兄弟文件夹中创建新文件}}。有人能告诉我为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

我的建议是不要使用

Gdx.files.internal(folder + "/" + name);

如果必须使用File api,请按以下方式执行:

Gdx.files.internal(new File(folder, name).toString());

这样可以避免路径分隔符可能发生的奇怪事情。

如果由于某种原因Gdx可能需要相对路径(可能相对于某些Gdx内部主目录),您可以使用NIO来执行类似

的操作
final Path gdxHome = Paths.get("path/to/gdx/home");

//...

File combined = new File(folder, name);
String relativePath = gdxHome.relativize(combined.toPath()).toString();

答案 1 :(得分:0)

好的,经过激烈的测试,我发现了问题,我觉得这很荒谬。

由于该文件为Internal,这意味着new File(...)引用无法正确发布,而是InputStream(如果我是&# 39;正确),但无论如何,使用FileHandle#file()文件上的方法Internal会导致路径进行某种转换,因此在删除任何以FileHandle#file()Internal处理的内容后1}}文件修复了它。