简体   繁体   English

使用getResources时未找到FileSystem异常

[英]FileSystem not found exception when using getResources

I am very lost with the concept of getResources 我对getResources的概念非常迷失

I have put a simple text file in a bin folder which I would like to access as a resource so I can then build and deploy. 我已经将一个简单的文本文件放在bin文件夹中,我希望将其作为资源进行访问,以便随后进行构建和部署。 However when I try to run the jar file I get a file not found error which I think is down to how I am accessing the resource. 但是,当我尝试运行jar文件时,出现文件未找到错误,我认为这取决于我如何访问资源。 Can anyone give me some direction as to how to use it. 谁能给我一些使用方法的指导。

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);
        }   
    }
}

The error I get 我得到的错误

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)

**Edit with @BorisTheSpiders Answer **使用@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);
            }               

but Im not really sure how I then use the reader to provide a Paths.get with a uri . 但是我不太确定如何使用阅读器为uri提供Paths.get I think I'm probably not understanding something fundamental here... 我想我可能不了解这里的基本知识...

As pointed out in the comments, the file in question cannot be found in the file system. 如评论中所指出,在文件系统中找不到相关文件。

As a suggestion, try replacing 建议,尝试更换

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

with

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

and the block where the file is read with the following: 以及使用以下命令读取文件的块:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否为contextWrapper.getResources()提供了空指针异常? 使用自定义侦听器时 - null pointer exception for contextWrapper.getResources()? when using custom listener 为我的 zip 创建文件系统时未找到提供程序异常? - Provider not found exception when creating a FileSystem for my zip? getResources()上的nullPoint异常 - nullPoint Exception on getResources() 使用getResources()。openRawResource()时是否有避免大小限制的方法 - Is there a way to avoid size limit when using getResources().openRawResource() 尝试使用此处广泛提倡的方法在非活动方法中执行 getResources 时,“无法解析方法 getResources” - "Cannot resolve method getResources" when trying to execute getResources in non-activity method using method widely advocated here 使用资源getResources(); 在AppWidget Android中 - Using Resources getResources(); in AppWidget Android 使用(hadoop)FileSystem时,Spark应用程序中的InvalidProtocolBufferException - InvalidProtocolBufferException in spark app when using (hadoop) FileSystem 使用 getCurrentSession() 时出现“No Session found for current thread”异常 - Getting “No Session found for current thread” exception when using getCurrentSession() 在C#中使用Java类时找不到类异常 - Class not found exception when using Java class in c# 使用XPath进行XML解析时找不到文件异常 - File not found exception when using XPath for XML parsing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM