The Java curriculum design of the pit Daddy, to use Jar to run
Low support for reading and storing files within jar
Storage method:
1 Importjava.io.IOException;2 ImportJava.io.InputStream;3 ImportJava.net.URL;4 5 Public classtestdouble {6 Public Static voidMain (string[] args)throwsIOException {7URL url=NewURL ("Jar:file:/c:/test.jar!/a.txt"); 8 System.out.println (URL);9InputStream is=Url.openstream ();Ten byteb[]=New byte[1000]; One Is.read (b); ASystem.out.println (NewString (b). Trim ()); - } -}
If you want to not rely on an absolute path, first get the current absolute path when running the jar, and then add the directory you want to read
Change the way inside.
String jarpath = Main. class . Getprotectiondomain (). Getcodesource (). GetLocation (). GetFile (); URL url=new
About storage. The design concept of the jar is a read-only file, a Java document, a java.util.jar.JarOutputString, but not very useful
A super-easy way for Java to read files within a jar