我正在尝试使用下面的代码从资源目录中读取文件
new FileInputStream(new File(getClass().getClassLoader().getResource(keyFile).getFile()))
运行时出现异常
java.io.FileNotFoundException: file:\D:\WorkSpace\server\target\server.jar!\BOOT-INF\classes!\config\key.pgp (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
最佳答案
根据您尝试获取的资源在 jar 中的位置,您应该使用相对路径来获取资源。您还可以通过使用 ResourceAsStream 直接以 InputStream 形式请求资源来完全跳过 File 对象。方法:
InputStream in = getClass().getResourceAsStream("/config/key.pgp");
关于java - 从资源目录读取文件时出现 FileNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53958275/