Reading a text file from a jar

前端 未结 3 912
慢半拍i
慢半拍i 2020-12-07 06:07

I\'m having trouble reading a text file from inside a jar. I managed it while it was unzipped but now that I\'ve archived it it won\'t work. The jar has:

  • BTA.j
3条回答
  •  日久生厌
    2020-12-07 06:21

    A jar is just a zip file and can be read like this..

    ZipFile file = new ZipFile("/home/Desktop/myjar.jar");
    Enumeration entries = file.entries();
    
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
    
        if (entry.getName().startsWith("splash")) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream(entry)))) {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        }
    }
    

提交回复
热议问题