Tag: jar输入

运行.jar时获取getResourceAsStream文件路径

我的代码: BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream(“playerhit.mp3”)); 当playerhit.mp3文件与运行它的MP3.class在同一个包中时,此代码可以正常工作。我将其运行为.jar。 如果我将文件路径更改为类似/src/data/audio/playerhit.mp3它就不再起作用了。 在以.jar身份运行时,是否仍然可以访问不同于文件包根目录的文件路径?

当jar文件从URL打开为InputStream时,JarEntry.getSize()返回-1

我试图从JarInputStream读取文件,文件的大小返回-1。 我从URL访问Jar文件作为inputStream。 URLConnection con = url.openConnection(); JarInputStream jis = new JarInputStream(con.getInputStream()); JarEntry je = null; while ((je = jis.getNextJarEntry()) != null) { htSizes.put(je.getName(), new Integer((int) je.getSize())); if (je.isDirectory()) { continue; } int size = (int) je.getSize(); // -1 means unknown size. if (size == -1) { size = ((Integer) htSizes.get(je.getName())).intValue(); } byte[] b = new […]

Java InputStream无法从JAR中读取文件

我试图从我的可执行文件.jar文件中读取一个文件,但它不断获取空值。 我有这个代码: public DanceEventTicketScanner(String txtfile){ sv = new ScannerView(this); findcode = false; InputStream is = this.getClass().getResourceAsStream(“/resources/copy.csv”); if (is == null) JOptionPane.showMessageDialog(null, “Resource not located.”); } 在JAR文件中,我(正常情况下)包含所有.class文件的文件夹,并在同一目录中包含一个名为resources的文件夹,其中包含copy.csv文件。 但是,此代码无法识别该文件。 有没有人有任何想法?