How to read, write file inside the JAR file?

Hi all,

I want to read the file inside the jar file, use following method:

File file = new File("filename");

........

.......

It works if not in JAR file, but it doesn't work if it's in a JAR file.

I found someone had the same problem, but no reply.

http://forum.java.sun.com/thread.jsp?forum=22&thread=180618

Can you help me ? I have tried this for all night !

Thanks in advance

Leo

[463 byte] By [LuckyLeo] at [2007-9-26 13:37:47]
# 1
You can create a ZipFile object and then read the files inside using the ZipInputStream.
jquattro at 2007-7-2 14:17:50 > top of Java-index,Desktop,Deploying...
# 2
You could also use something like this:JarFile jf = new JarFile("myJar.jar");InputStream is = jf.getInputStream(jf.getEntry("filename")); -Ron
rSully at 2007-7-2 14:17:50 > top of Java-index,Desktop,Deploying...
# 3
If you want to read a file from the JAR file that the application is packaged in (rather than a separate external JAR file) you do it like this ...InputStream is = ClassLoader.getSystemResourceAsStream("filename");
ianfallon at 2007-7-2 14:17:50 > top of Java-index,Desktop,Deploying...
# 4

> If you want to read a file from the JAR file that the

> application is packaged in (rather than a separate

> external JAR file) you do it like this ...

>

> InputStream is =

> ClassLoader.getSystemResourceAsStream("filename");

Better to use

this.getClass().getClassLoader().getResourceAsStream();

From a class near to where the data is. This deals with multiple classloaders properly.

slo at 2007-7-2 14:17:50 > top of Java-index,Desktop,Deploying...