gpt4 book ai didi

java - Spring Boot - 访问资源文件夹内的文件

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:19 25 4
gpt4 key购买 nike

我正在从主类访问资源文件夹内的文件

File file = new ClassPathResource("remoteUnitsIdsInOldServer.txt").getFile();

我收到此错误:

java.io.FileNotFoundException: class path resource [remoteUnitsIdsInOldServer.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/lopes/Documents/workspace-sts-3.9.0.RELEASE/telefonicaUtils/target/telefonicaUtils-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/remoteUnitsIdsInOldServer.txt

我什至打开jar 文件,文件remoteUnitsIdsInOldServer.txt 就在那里,在类内部

最佳答案

对我来说最简单的解决方案是,

try {
ClassPathResource classPathResource = new ClassPathResource("remoteUnitsIdsInOldServer.txt");
byte[] data = FileCopyUtils.copyToByteArray(classPathResource.getInputStream());
String content = new String(data, StandardCharsets.UTF_8);
} catch (Exception ex) {
ex.printStackTrace();
}

关于java - Spring Boot - 访问资源文件夹内的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com