使用getResources时未找到FileSystem异常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用getResources时未找到FileSystem异常相关的知识,希望对你有一定的参考价值。

我迷失了getResources的概念

我已将一个简单的文本文件放在bin文件夹中,我想将其作为资源访问,以便我可以构建和部署。但是,当我尝试运行jar文件时,我得到一个文件未找到错误,我认为这取决于我如何访问该资源。任何人都可以给我一些指导如何使用它。

public class Iterator {
    static ArrayList<String> myFiles = new ArrayList<String>();
    static URL filename= Iterator.class.getResource("/Files/FilesLogged.txt");
    static String folderName;
    static Path p;

    public Iterator() { }

    public static void main(String[] args) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException, BackingStoreException {       

        Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class);

        p = Paths.get(filename.toURI());
        //This iterates through each of the files in the specified folder and copies them to a log. 
        //It also checks to see if that file has been read already so that it isn't re-inputted into the database if run again               
        //Loop through the ArrayList with the full path names of each folder in the outer loop
        for (String line : Files.readAllLines(p)){
            myFiles.add(line);
        }   
    }
}

我得到的错误

Exception in thread "main" java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)
at Overview.Iterator.main(Iterator.java:46)

**使用@BorisTheSpiders回答编辑

public class Iterator {
    static ArrayList<String> myFiles = new ArrayList<String>();

    static URL filename= Iterator.class.getResource("/Files/FilesLogged.txt");
    static String folderName;
    static Path p;
    public Iterator() {
    }

    public static void main(String[] args) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException, BackingStoreException {       

        Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class);
        InputStream in = filename.openStream( );
        BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
             p = Paths.get(filename.toURI());
            //This iterates through each of the files in the specified folder and copies them to a log. 
            //It also checks to see if that file has been read already so that it isn't re-inputted into the database if run again               
            //Loop through the ArrayList with the full path names of each folder in the outer loop
            for (String line : Files.readAllLines(p)){
                myFiles.add(line);
            }               

但我不确定如何使用读者提供Paths.geturi。我想我可能不会理解一些基本的东西......

答案

正如评论中指出的那样,在文件系统中找不到有问题的文件。

作为建议,尝试更换

static URL filename= Iterator.class.getResource("/Files/FilesLogged.txt");

static InputStream is = Iterator.class.getResourceAsStream("/Files/FilesLogged.txt");

以及使用以下内容读取文件的块:

try (Scanner scanner = new Scanner(is)) {
   while(scanner.hasNextLine()){
        String line = scanner.nextLine();
        myFiles.add(line);
    }
}

以上是关于使用getResources时未找到FileSystem异常的主要内容,如果未能解决你的问题,请参考以下文章

使用 asihttprequest 访问 Web 服务时未找到方法错误

使用 cocoapods 时未通过桥接头找到 Flurry.h

使用 TBB 时未找到 msvcp80d.dll

尝试在 ListView 中使用其内容时未找到 Flutter FutureProvider

制作 .ipa 时未使用 Xcode Server Bot 找到代码签名身份

尝试启动 Atom/Nuclide 时未找到流