我的 java 测试代码尝试访问我的项目文件夹中的文件
但是我得到了 IO 异常:
String fileContents = new String(Files.readAllBytes(
Paths.get("src/test/resources/SenegalAndBulgariaConfig.txt")));
String fileContents = new String(Files.readAllBytes(
Paths.get("src/")));
它曾经有效,但现在不行了:
java.nio.file.NoSuchFileException: src/test/resources/SenegalAndBulgariaConfig.txt
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.Files.readAllBytes(Files.java:3152)
虽然它存在:
最佳答案
你永远不应该依赖文件
。从类路径加载InputStream
。
如果使用 Maven 标准目录结构,src/main/resources 和 src/test/resources 都在类路径中。
我倾向于喜欢 Apache Commons IO 库和 Spring ClassPathResource
。
关于java - 在路径 ""src/test/resources/上找不到文件”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50025712/