2014-11-25 118 views
0

我做了一个包装ConfigurationFile类来帮助处理Gdx.files的东西,它很长时间工作,但现在它不工作,我不知道为什么。Gdx.files.internal(...)包装不能正常工作

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

-Snip现在不必要的信息 -


UPDATE
更多的配置之后,我才来的,他们不是在表现相同。我有一个 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的兄弟文件夹中创建新文件。有人能告诉我为什么会发生这种情况吗?

+0

发布更新了我拥有的最新信息。 – CoderMusgrove 2014-11-26 16:39:48

回答

0

好了,经过紧张的测试相对路径,我发现了问题,我发现这很荒谬。

由于文件是Internal,这意味着一个new File(...)参考,不能正确地发微博,而是它是一个InputStream(如果我是正确的),但不管怎么说,使用上Internal文件的方法FileHandle#file()导致一些类型的路径转换,所以删除任何与FileHandle#file()处理Internal文件修复它。

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

这可能是一回事,但要求'(文件夹,字符串名称)'作为参数的'internal'和'external'方法只是在那里坐的。我只是使用当前的路径来注册我的文件,所以这没有什么区别。 – CoderMusgrove 2014-11-25 03:00:17