Javaio--java How to read the property file inside the jar bundle itself __java

Source: Internet
Author: User
A JAR file is a standard way to package a solution based on JAVA technology. It allows developers to package all relevant content (. class, pictures, sounds, and all supported files ) into a single file. The jar format supports compression, authentication, version numbers, and many other features.
  
Reading a file from a jar file is a hassle, but it's not necessarily the case. This article will show you how to read a file from a jar file, first, get the file directory in the jar file, and then do a specific file operation.

  
If you are familiar with the commonly used zip format, the jar file is similar. A jar file provides a way to package multiple files into a single file, where each file may be compressed independently. The added content of the jar file is manifest, which allows developers to provide additional information about the content. For example, manifest indicates which file in the jar file is used to run a program, or the version number of the library, and so on

J2SEDK provides a jar tool that you can use to read and write jar files from the console. However, if you need to read and write the jar file in the program, it may take a little time (this article contains only how to read and write the jar file in the program). The good news is that you can do this, and you don't have to worry about unpacking, because the class library will help you do that. The class files you need are located in the Java.util.jar package. The main class here is Jarfile, which is a reference to the. jar file itself. Each individual file in a larger file is referenced by a jarentry.
  
First, a Jarfile instance is created by passing the location of the jar file to the constructor, which may be in the form of string or file, as follows: Jarfile jarfile = new Jarfile ("Thefile.jar");
  
Or:
  
File File = new file ("Thefile.jar");
Jarfile jarfile = new Jarfile (file);

  
You may notice that when a file is not in class path, the Jarfile class is useful for reading a file file from a jar.
When you want to specify a target jar file, the Jarfile class is also useful for reading files from the jar. Of course, if the jar file is in class path, the method of reading the file from it is simpler, and you can use the following method:
  
   
URL url = classloader.getsystemresource (name);
Or
InputStream Stream =classloader.getsystemresourceasstream (name);
       
This technique allows you to read files from a jar file in class path. You do not have to specify the jar filename.
  
  
There are other constructors, such as authentication support, flags to delete files, and so on. These constructors are not described here.
  
Once you have a reference to the jar file, you can read the directory information in its file contents. The Jarfile entries method returns an enumeration collection (enumeration) for all entries. With each entry, you can get its properties , any authentication information, and any other entry information, such as its name or size, from its manifest file.
enumeration enum = Jarfile.entries ();
while (Enum.hasmoreelements ()) {
Process (Enum.nextelement ());
}
  
As mentioned earlier, each entry is a jarentry. The class has getname,getsize,getcompressedsize and other methods.
  
Below, let's illustrate how to use these features in a program. The following program shows the name, size, and size of the content in the jar file you specified (which is somewhat similar to specifying "T" and "V" options when using the jar command).
  
   
  
     Import java.io.*;
Import java.util.*;
Import java.util.jar.*;
public class Jardir {public
static void Main (String args[])
throws IOException {
if (Args.len      Gth!= 1) {
System.out.println (
"Please provide a JAR filename");
System.exit ( -1);
}
jarfile jarfile = new Jarfile (args[0]);
enumeration enum = Jarfile.entries ();
while (Enum.hasmoreelements ()) {
process (enum.nextelement ());
The
private static void process (Object obj) {
Jarentry entry = (jarentry) obj;
String name = Entry.getname ();
Long size = Entry.getsize ();
Long compressedsize = Entry.getcompressedsize ();
System.out.println (
name + "T" + size + "\ T" + compressedsize);
}
}    
If you run the above Jardir program using the Jce.jar in j2se1.4.1, you will see output similar to the following Meta-inf/manifest. MF 5315 1910
Meta-inf/4jcejars. SF 5368 1958
Meta-inf/4jcejars. DSA 2207 1503
meta-inf/0 2
javax/0 0
javax/crypto/0 0
javax/crypto/interfaces/0 0
Javax/crypto/interfaces/dhkey.class 209 185
Javax/crypto/interfaces/dhpublickey.class 265 215
Javax/crypto/interfaces/dhprivatekey.class 267 215
Javax/crypto/interfaces/pbekey.class 268 224
Javax/crypto/secretkey.class 167 155
...
  
  
  Note the first meta-inf line of output. This is manifest and security authentication information. The 0-byte entries is not a file, but it is a bit equivalent to a directory.
  
In order to actually read a specified file from a jar file, you must go to its entry inputstream. This is not the same as jarentry. This is because Jarentry only contains information about the entry, but it does not actually contain the contents of the entry. This is a bit like the difference between file and FileInputStream. Access to the file does not open the file, it simply reads the file information from the directory. Here's how to get entry's InputStream:
  
  

InputStream input = Jarfile.getinputstream (entry);


  
When you have an input stream, you can read it as if you were reading another stream. In the text stream, remember to use Reader (reader) to get characters from the stream. For byte-oriented streams, such as picture files, it's OK to read directly.
  
The following program shows how to read a file from a jar file. Specifies the name of the jar file, the name of the file to be read (a file in the packaged jar file) as a parameter to invoke the program. The file to be read should have a text type.
  
   

     Import java.io.*;
Import java.util.jar.*;
public class Jarread {public
static void Main (String args[])
throws IOException {
if (args.le      Ngth!= 2) {
System.out.println (
"Please provide a JAR filename and file to read");
System.exit ( -1);
}
jarfile jarfile = new Jarfile (args[0]);
Jarentry entry = Jarfile.getjarentry (Args[1]);
InputStream input = Jarfile.getinputstream (entry);
process (input);
Jarfile.close ();
private static void process (InputStream input)
throws IOException {
Inputstreamreade     R ISR =
New InputStreamReader (input);
BufferedReader reader = new BufferedReader (ISR);
String Line;
while (line = Reader.readline ())!= null) {
System.out.println (line);
}
Reader.close ();
}
}    


Suppose there is a spider.txt file in the Myfiles.jar file, the contents of the spider file are as follows:

The Itsy bitsy Spider
Ran up the water spout
Down came the rain and
washed the spider out

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.