【发布时间】: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();
}
}
}
我难以理解的是这两种方式之间的区别是什么导致我的ConfigurationFile 在assets 的同级文件夹中创建一个新文件。有人能告诉我为什么会这样吗?
【问题讨论】:
-
帖子更新了我拥有的最新信息。
标签: java file libgdx bufferedreader