С помощью getResourceAsStream(path) я получаю sun.net.www.protocol.jar.JarURLConnection $JarURLInputStream. Но этот поток кажется пустым, когда путь является каталогом.
Это мой тестовый код:
Код: Выделить всё
package de.CoSoCo.testzone;
import java.io.*;
/**
*
* @author Ulf Zibis
* @version
*/
public class ResourceAsStream {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String packagePath = ResourceAsStream.class.getPackageName().replace('.', '/');
String path;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class c = ResourceAsStream.class;
InputStream in;
byte [] bytes = new byte[16];
try {
System.out.println("path="+(
path = packagePath));
System.out.println("Class="+(
in = cl.getResourceAsStream(path)));
BufferedReader r = new BufferedReader(new InputStreamReader(in));
System.out.println("entries=");
for (String line; (line = r.readLine()) != null; )
System.out.println(" "+line);
in = cl.getResourceAsStream(path);
System.out.println("read bytes="+in.read(bytes));
} catch (Exception ex) { System.out.println(ex); }
try {
System.out.println("path="+(
path = packagePath+'/'+"A_Picture.png"));
System.out.println("Class="+(
in = cl.getResourceAsStream(path)));
System.out.println("read bytes="+in.read(bytes));
} catch (Exception ex) { System.out.println(ex); }
try {
System.out.println("path="+(
path = ""));
System.out.println("Class="+(
in = c.getResourceAsStream(path)));
BufferedReader r = new BufferedReader(new InputStreamReader(in));
System.out.println("entries=");
for (String line; (line = r.readLine()) != null; )
System.out.println(" "+line);
in = c.getResourceAsStream(path);
System.out.println("read bytes="+in.read(bytes));
} catch (Exception ex) { System.out.println(ex); }
try {
System.out.println("path="+(
path = "A_Picture.png"));
System.out.println("Class="+(
in = c.getResourceAsStream(path)));
System.out.println("read bytes="+in.read(bytes));
} catch (Exception ex) { System.out.println(ex); }
}
}
Код: Выделить всё
path=de/CoSoCo/testzone
Class=java.io.ByteArrayInputStream@4e50df2e
entries=
A_Picture.png
ClassFinder.class
ClassFinder$1.class
FileChooserDemo.class
FileTimesFromCalendar.class
ResourceAsStream.class
read bytes=16
path=de/CoSoCo/testzone/A_Picture.png
Class=java.io.BufferedInputStream@3941a79c
read bytes=16
path=
Class=java.io.ByteArrayInputStream@506e1b77
entries=
A_Picture.png
ClassFinder.class
ClassFinder$1.class
FileChooserDemo.class
FileTimesFromCalendar.class
ResourceAsStream.class
read bytes=16
path=A_Picture.png
Class=java.io.BufferedInputStream@4fca772d
read bytes=16
Код: Выделить всё
$ java -jar TestZone.jar
path=de/CoSoCo/testzone
Class=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@3d4eac69
entries=
read bytes=-1
path=de/CoSoCo/testzone/A_Picture.png
Class=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@135fbaa4
read bytes=16
path=
Class=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@330bedb4
entries=
read bytes=-1
path=A_Picture.png
Class=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@7ea987ac
read bytes=16
Моя цель — < strong>прочитать записи каталога.
Подробнее здесь: https://stackoverflow.com/questions/786 ... tream-java
Мобильная версия