【发布时间】:2021-10-03 03:18:12
【问题描述】:
我创建了一个插件。它需要从 res 文件夹中访问 2 个文件。现在这里的技巧是, res 文件夹不在插件中。例如,我已经在本地发布了插件,因此它现在可以在您创建的任何项目下的工具菜单中看到。我想要的是,我创建的任何新项目,它将在 res 文件夹中有 2 个文件 facility.json 和 groups.json,现在我的插件应该能够访问这些文件。我尝试了如下所示的不同方法,但它总是失败,说“找不到文件”异常。如果有人可以帮助我解决这个问题。其赞赏。谢谢。
private static File getFileFromResource(String fileName) throws URISyntaxException {
ClassLoader classLoader = Validator.class.getClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
// failed if files have whitespaces or special characters
//return new File(resource.getFile());
return new File(resource.toURI());
}
}
private static InputStream getFileFromResourceAsStream(String fileName) {
// The class loader that loaded the class
ClassLoader classLoader = Validator.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
// the stream holding the file content
if (inputStream == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
return inputStream;
}
}
【问题讨论】:
标签: java intellij-idea intellij-plugin intellij-15