Read the JAR Package 1

Source: Internet
Author: User

---restore content starts---

Yesterday when doing the project plugin, because a file in the jar package will be used to initialize the program. And then you will still be accessing this file, so think of simply copy the file to the specified directory. At the time of the copy also took a good time, here involves the operation of the jar file, in here to write down the useful when it is convenient to find

  1. If a jar or other compressed package exists in the jar, this is the way to read
    1. 1  Public classjarfileaccess {2 3     Private Static FinalString fileseparator = System.getproperty ("File.separator");4     /**5      * 6      * @paramJarfilename The name of the jar file, (note to add a ". jar" suffix, without any path separators)7      * @paramFromdir The path of the jar8      * @paramTodir The path to copy the file to the specified location9      * @throwsExceptionTen      */ One      Public voidAccessjarfile (String jarfilename, String Fromdir, String todir)throwsexception{ AJarfile Myjarfile =NewJarfile (fromdir+fileseparator+jarfilename); -Enumeration MyEnum =myjarfile.entries (); -      while(Myenum.hasmoreelements ()) { theJarentry Myjarentry =(Jarentry) myenum.nextelement (); - System.out.println (Myjarentry.getname ()); -       if(Myjarentry.getname (). Equals ("Config.jar")){ -InputStream is =Myjarfile.getinputstream (myjarentry); +FileOutputStream fos =NewFileOutputStream (todir+fileseparator+myjarentry.getname ()); -         byte[] B =New byte[1024]; +         intLen; A          while(len = Is.read (b))! =-1){ at System.out.println (b.tostring ()); -Fos.write (b, 0, Len); -         } - fos.close (); - is.close (); -          Break; in}Else{ -         Continue; to       } +     } - myjarfile.close (); the   } *}

  2. If the file to be read does not exist as a compressed package or jar in the jar package, use the following method to conveniently point
    1. 1  Public classjarfileaccess{2 /**3 * @function Read the contents of the specified file in the jar package and return as a string4        * @parampath to the Jarpath jar file5        * @paramName of the file to be read, to add the suffix name6        * @returnString Returns the information read to7        * @throwsIOException8        */9        PublicString Readfilefromjar (String jarpath, string name)throwsIOException {TenJarfile JF =NewJarfile (Jarpath);  OneEnumeration<jarentry> JFS =jf.entries ();  AStringBuffer SB =NewStringBuffer ();  -              while(Jfs.hasmoreelements ()) -             {   theJarentry JFN =jfs.nextelement ();  -                 if(Jfn.getname (). EndsWith (name)) -                 {   -InputStream is =Jf.getinputstream (JFN);  +Bufferedinputstream bis =NewBufferedinputstream (IS);  -                     byte[] buf =New byte[Is.available ()];  +                      while(Bis.read (BUF)!=-1)   A                     {   atSb.append (NewString (BUF). Trim ());  -                     }   - Bis.close ();  - Is.close ();  -                      Break;  -                 }   in             }   -             returnsb.tostring ();  to         }   +       /** - * @function Read the contents of the specified file in the jar package and copy the contents of the read to the specified file the        * @parampath to the Jarpath jar file *        * @paramName of the file to be read, to add the suffix name $        * @paramTonewfile Copying the copied information to the destination filePanax Notoginseng        * @throwsIOException -        */ the        Public  voidReadfilefromjar (String jarpath, String Name,file tonewfile)throwsIOException { +Jarfile JF =NewJarfile (Jarpath);  AEnumeration<jarentry> JFS =jf.entries ();  theStringBuffer SB =NewStringBuffer (); +              while(Jfs.hasmoreelements ()) -             {   $Jarentry JFN =jfs.nextelement ();  $                 if(Jfn.getname (). EndsWith (name)) -                 {   -InputStream is =Jf.getinputstream (JFN); theFileOutputStream fos =NewFileOutputStream (tonewfile); -Bufferedinputstream bis =NewBufferedinputstream (IS); Wuyi                     byte[] buf =New byte[Is.available ()];  the                      while(Bis.read (BUF)!=-1)   -                     {   Wu Fos.write (BUF); -                         About                     }   $ fos.close (); - Bis.close ();  - Is.close ();  -                      Break;  A                 }   +             }   the             -         }   $        the}

---restore content ends---

Read the JAR Package 1

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.