我該如何解決這種情況?我有一個靜態 JSON 檔案src/main/resources,當我運行代碼時,我使用這個 JSON 來創建一些資料,但是當我嘗試部署時,heroku我得到了這個錯誤,可能是因為他們找不到FUNDOS.json檔案。
有誰知道如何解決這個問題?
代碼
String FUNDOS_JSON = "src/main/resources/data/FUNDOS.json";
Path filePath = Path.of(FUNDOS_JSON).toAbsolutePath();
String jsonString = Files.readString(filePath);
JSONArray json = new JSONArray(jsonString);
錯誤
remote: [ERROR] COMPILATION ERROR :
remote: [INFO] -------------------------------------------------------------
remote: [ERROR] /tmp/build_835ef365/src/main/java/br/anbima/mvc/service/AnbimaServiceImpl.java:[36,37] cannot find symbol
remote: symbol: method of(java.lang.String)
remote: location: interface java.nio.file.Path
remote: [ERROR] /tmp/build_835ef365/src/main/java/br/anbima/mvc/service/AnbimaServiceImpl.java:[37,42] cannot find symbol
remote: symbol: method readString(java.nio.file.Path)
remote: location: class java.nio.file.Files
remote: [INFO] 2 errors
uj5u.com熱心網友回復:
對于 src/main/resources 中的資產,請使用getResourceAsStream或getResource。
假設您的代碼位于名為 Foo.java 的檔案中,
InputStream fundosStream = Foo.class.getResourceAsStream("/data/FUNDOS.json");
String fundosContent = "";
try(BufferedReader br = new BufferedReader(new InputStreamReader(fundosStream)) {
String line = br.readLine();
while(line != null) {
fundosContent = line "\n";
line = br.readLine();
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
JSONArray jsonArray = new JSONArray(fundosContent);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/490425.html
