2016-11-21 50 views
0
try { 

     byte[] encoded = Files.readAllBytes(Paths.get(getClass().getResource("/files/game.json").getFile())); 
     return new String(encoded); 

    } catch (Exception e) { 
     return ""; 
    } 

此塊在osx上工作,但是當我嘗試在Windows上運行它時,它不會找到相對路徑。在OSX和窗口上的相對路徑差異

回答

0

Windows使用 「\」,但因爲 「\」 是轉義字符只使用雙反斜線:

().getResource("\\files\\game.json") 

我希望這個作品!

+0

很好的答案,但它只是返回相同的錯誤。 我可以補充一點,那就是當我用windows電腦的 上的直接路徑提供readAllBytes()時,它會按照它應該做的。 –

+0

哦,所以我不能幫你了我還是一個初學者:)對不起 – WasteD

+0

感謝您嘗試雖然 –

0
private String getGameJSONFile() throws IOException { 

    byte[] bytePath = new File("src/files/game.json").getCanonicalPath().getBytes(); 

    String path = new String(bytePath); 

    Path p = Paths.get(path); 

    byte[] encoded = Files.readAllBytes(p); 

    return new String(encoded); 
} 

如果有人有興趣,這並獲得成功