有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaGDX。文件夹。内部(…)包装器工作不正常

我制作了一个包装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的同级文件夹中创建一个新文件。有人能告诉我为什么会这样吗


共 (1) 个答案

  1. # 1 楼答案

    好的,经过严格的测试,我发现了这个问题,我觉得很可笑

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