我正在尝试在我的插件中获取JSON,但是它不起作用。这是我要获取的代码,这是路径:static File json = new File("config.JSON");
。
public class JSONReader extends JavaPlugin {
static File json = new File("config.JSON");
static File getJSON() {
return json;
}
static JSONObject setupJson() {
try {
JSONParser jsonParser = new JSONParser();
Object parsed = jsonParser.parse(new FileReader(json.getPath()));
JSONObject jsonObject = (JSONObject) parsed;
return jsonObject;
} catch (ParseException | IOException e) {
return null;
}
}
}
所有路径都相对于工作目录进行评估,这是包含服务器可执行文件的路径。 Bukkit提供了通过JavaPlugin
访问文件的便捷方法:
参考插件数据文件夹中的文件
JavaPlugin
在插件的jar中检索文件
File f = new File(getDataFolder(), "config.json");
该路径是相对于Bukkit服务器jar的,因此您需要从那里转到文件InputStream is = getResource("config.json");