gpt4 book ai didi

java 9资源文件不同模块NoSuchFileException

转载 作者:行者123 更新时间:2023-11-30 02:30:41 28 4
gpt4 key购买 nike

我正在尝试加载资源文件夹中存在的文件,但出现异常。我正在使用java 9,并且具有读取文件的代码的java文件存在于其他一些模块中,并且调用代码位于其他一些模块中。有人可以建议如何继续吗?

enter image description here

异常堆栈跟踪

java.nio.file.NoSuchFileException: genome-tags.csv
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:231)
at java.base/java.nio.file.Files.newByteChannel(Files.java:364)
at java.base/java.nio.file.Files.newByteChannel(Files.java:410)
at java.base/java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.base/java.nio.file.Files.newInputStream(Files.java:154)
at java.base/java.nio.file.Files.newBufferedReader(Files.java:2809)
at java.base/java.nio.file.Files.readAllLines(Files.java:3239)
at java.base/java.nio.file.Files.readAllLines(Files.java:3279)
at com.bhargo.filesystem.reader/com.bhargo.filesystem.reader.FileSystemReader.read(FileSystemReader.java:15)
at com.bhargo/com.bhargo.Main.main(Main.java:20)

代码是:-

    public class Main {

public static void main(String[] args) {
ServiceLoader<IReader> serviceLoader = ServiceLoader.load(IReader.class);
try {
System.out.println(serviceLoader.iterator().next().read("genome-tags.csv", Main.class).size());
} catch (IOException e) {
e.printStackTrace();
}
}
}


public interface IReader {
List<String> read(String fileLocation, Class clazz) throws IOException;

default File getFile(String fileLocation, Class clazz) {
URL url = clazz.getResource(fileLocation);
return new File(getClass().getClassLoader().getResource(fileLocation).getFile());
}
}


public class FileSystemReader implements IReader{
@Override
public List<String> read(String fileLocation, Class clazz) throws IOException {
return Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getName()));
}
}

谢谢

阿马尔

最佳答案

改变

public class FileSystemReader implements IReader{
@Override
public List<String> read(String fileLocation, Class clazz) throws IOException {
return Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getName()));
}
}

public class FileSystemReader implements IReader{
@Override
public List<String> read(String fileLocation, Class clazz) throws IOException {
return Files.readAllLines(Paths.get(getFile(fileLocation, clazz).getAbsolutePath()));
}
}

关于java 9资源文件不同模块NoSuchFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357152/

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