http://blog.csdn.net/silentbalanceyh/article/details/5252285
(This chapter will talk about a more important chapter in Java, here to say sorry, because the reason for the recent change of work, there has been no time to continue writing tutorials, but then I will always adhere to the HA, I hope you can support.) This section mainly deals with commonly used file reads and writes, including advanced file IO content--java.nio, which is a fairly common part of some of today's projects, and if there are any omissions or clerical mistakes, I would like the reader to email us:silentbalanceyh@126.comThank you. This section may be much longer than the previous chapters, but also to ensure that the Java inside IO and file operations can be written to write all, if there are omissions hope that the reader to email, the concept of confusion in the place please tell, some of the contents of the reference to some of the original data translation and thinking notes. )
The contents of this chapter:1.IO class related content 2. Files and directories3. Advanced file Operation "read/write (. jar) for jar document" jar File Description:The jar file format is based on the popular ZIP file format, used to compress many files into a single file, unlike zip files, jar files are used not only for compression and publishing, but also for deploying and encapsulating libraries, components, and plug-ins, and can be used directly by tools such as compilers and JVMs. Include special files in the jar file, such as manifests and deployment descriptors, to just how the tool handles specific jar files.the function of the jar file is as follows:The deployment unit used to publish and use the class library as an application and an extension of a component, applet, or plug-in to package the auxiliary resource JAR file format associated with the component provides many advantages and functionality, many of which are not provided by traditional compression formats such as ZIP or tar, including:Security:You can digitally sign the contents of the jar file so that the tool that recognizes the signature can selectively grant you software security privileges that other files cannot, and it can also detect if the code has been tampered withreduce download time:If an applet is bundled into a jar file, the browser can download the applet's class file and related resources in an HTTP transaction instead of opening a new connection to each fileCompression:Jar format allows you to compress files to improve storage efficiencyTransport Platform Extensions:The Java extension Framework (Java Extensions Framework) provides a way to add functionality to the Java core platform that is packaged with jar files (Java and javamail are examples of extensions)Package Seal:Packages stored in JAR files can optionally be sealed to enhance versioning consistency and security, and sealing a package means that all classes in the package must be found in the same jar filePackage version control:A jar file can contain data about the files it contains, such as vendor and version informationPortability:The mechanism for handling jar files is the standard part of the Java core Platform APIMeta-inf Directory:Most jar files contain a meta-inf directory that is used to store packages and extended configuration data, such as security and version information, and the Java 2 platform identifies and interprets the following files and directories in the Meta-inf directory to configure applications, extend applications, and class loaders:MANIFEST. MF:This manifest file defines the data source associated with the extension and packageINDEX. LIST:This file is generated by the new option-I of the Jar tool, which contains the location information of the packages defined in the application or extension, which is part of the Jarindex implementation and is loaded by the class loader process.XXX.SF:This is the signature file for the jar file.placeholder XXX represents the signer XXX. DSA:The signature program block file associated with the signature file that stores the public signature used to sign the jar filePack200 class:The full name of the Pack200 class is: Java.util.jar.Pack200, this class is the JDK 1.5 after the class, the main role is to effectively compress the jar file, the implementation is based on the Java class-specific structure-merge the constant pool, remove misuse information, save the inline data structure, use variable length encoding, Select the optimized code type for two times compression to achieve efficient compression. However, this class is compressed for Java classes, so compression of normal files is no different from normal compression software, but it can easily reach the 10-40% compression rate for jar files, which is useful in Java application deployments, Especially for Java mobile applications compression and decompression is particularly good practice. Its use is mainly used in the case of a lot of class files, when the jar contains more than Java class resource files, such as JPEG or GIF, using the gzip format is the best choice, but if most of the jar package is class content, Using pack200 is absolutely preferred, because pack200 will be optimized for class Java classes, Pack200 compression and decompression speed is very fast, and the compression rate is amazing, try to know. The Java command line also provides related command tools: pack200 First provides a simple snippet and then examples of its operations:Compression:Pack200.packer packer = Pack200.newpacker (); OutputStream output = new Bufferedoutputstream (new FileOutputStream (Outfilename)); Packer.pack (New Jarfile (jarfile), output); Output.close ();Decompression:Pack200.unpacker Unpacker = Pack200.newunpacker (); OutputStream output = new Jaroutputstream (new FileOutputStream (Jarfile)); Unpacker.unpack (Pack200file,output); Output.close ();--[$]pack200 Class Example--Package Org.susan.java.io;
Import Java.io.File; Import Java.io.FileOutputStream; Import Java.io.OutputStream; Import Java.util.jar.JarFile; Import java.util.jar.Pack200;
Public Class pack200tester { public static void main (string args[]) throws exception{ jarfile file = new jarfile ("D:/work/study.jar"); pack200.packer packer = Pack200.newpacker (); outputstream out = new fileoutputstream ("D:/work/study.pack"); packer.pack (file, out); out.close (); file inputfile = new file ("D:/work/study.jar"); file outputfile = new file ("D:/work/study.pack"); system.out.println ("Before Pack Size:" + inputfile.length ()); system.out.println ("After Pack Size:" + outputfile.length ());  }} I have this output after this program is running: Before Pack Size: 293695 after Pack size:130423 "what needs to be explained is: If the jar itself is an executable jar, after being Pack200 compressed, if you want to execute the words must be decompressed to execute, otherwise this jar file will throw the error directly to tell you can not execute, That is, Pack200 for jar files is not a simple compression, is a highly efficient optimization, the main purpose is to make this jar after compression volume reduced, but the inside of the class can also be referenced, That is, if it's a Java library, this is a good way to provide a network jar. 】--[$]jar File List--Package Org.susan.java.io;
Import java.io.IOException; Import Java.sql.Date; Import java.util.Enumeration; Import java.util.jar.Attributes; Import Java.util.jar.JarEntry; Import Java.util.jar.JarFile;
Public Class jarlistreader { public static void main (string args[]) throws ioexception { jarfile file = new jarfile ("D:/work/study.jar"); Enumeration<JarEntry> e = file.entries (); while (e.hasmoreelements ()) { jarentry entry = E.nextelement (); system.out.println (Entry.getname ()); long uncompressedsize = Entry.getsize (); long compressedsize = Entry.getcompressedsize (); &NBSP;LONG&NBSP;CRC = ENTRY.GETCRC (); int method = Entry.getmethod (); &NBSP;&NBSP;&NBSp string comment = Entry.getcomment (); system.out.println (New date (Entry.gettime ())); system.out.println ("from" + uncompressedsize + ) Bytes to " + compressedsize); if (method = = zipentry.stored) { system.out.println ("zipentry.stored"); } else if (method = = zipentry.deflated) { system.out.println (zipentry.deflated); } System.out.println ("Its CRC is" + CRC); system.out.prinTLN (comment); system.out.println (Entry.isdirectory ());
attributes a = Entry.getattributes (); if (a!= null) { & nbsp; object[] namevaluepairs = A.entryset (). ToArray (); for (int j = 0; J < Namevaluepairs.length; J + +) { System.out.println (Namevaluepairs[j]);  } & nbsp; } system.out.println ();  }  }} This class reads the list of jar files, where I'm not going to enumerate the main contents of this file, just enumerate a few of them: Meta-inf/manifest. MF 2009-12-19 from bytes to 8 it CRC is 3423671674 null false
Org/susan/java/basic/breakcontinuemain.class 2009-12-18 from 924 bytes to 538 8 It CRC is 1903539533 null false but this does not Decompression, just a read process, this hope that readers should remember, if you want to extract, directly using the Zip decompression method can also operate.--[$] Gets the jar file from a URL address--Package Org.susan.java.io;
Import java.net.JarURLConnection; Import Java.net.URL; Import Java.util.jar.JarFile;
public class Jarmainentry {public static void main (String args[]) throws exception{//online address "Jar:http://hostna me/study.jar!/"url url = new URL (" jar:file:/d://work//study.jar!/"); Jarurlconnection con = (jarurlconnection) url.openconnection (); System.out.println (Con.getentryname ()); &