【问题标题】:Gdx.files.internal(...) wrapper not working correctlyGdx.files.internal(...) 包装器无法正常工作
【发布时间】:2014-11-25 02:17:12
【问题描述】:

我做了一个包装器 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();
        }
    }
}

我难以理解的是这两种方式之间的区别是什么导致我的ConfigurationFileassets 的同级文件夹中创建一个新文件。有人能告诉我为什么会这样吗?

【问题讨论】:

  • 帖子更新了我拥有的最新信息。

标签: java file libgdx bufferedreader


【解决方案1】:

我的建议是不要使用

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();

【讨论】:

  • 这可能是一回事,但要求 (File folder, String name) 作为参数的 internalexternal 方法现在只是坐在那里。我只是使用当前路径来注册我的文件,所以没有区别。
【解决方案2】:

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

由于文件是Internal,这意味着不能正确地对它进行new File(...) 引用,而是它是InputStream(如果我是正确的),但无论如何,使用方法@987654324 @ 在 Internal 文件上的 @ 会导致路径的某种转换,因此在删除与 FileHandle#file() 处理的任何内容后,Internal 文件修复了它。

【讨论】:

    猜你喜欢
    • 2021-02-19
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2021-12-13
    相关资源
    最近更新 更多