A super-easy way for Java to read files within a jar

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.