posts - 495,  comments - 11,  trackbacks - 0

          kk.jar的目錄結(jié)構(gòu)如下

          +kk.jar
          ??? +META-INF
          ??????? MANIFEST.MF
          ??? +config
          ??????? database.properties

          目標(biāo):讀取database.properties中的參數(shù)信息

          types=mysql
          mysql.url=jdbc:mysql://127.0.0.1:3306/tjtz
          mysql.user=root
          mysql.password=root
          mysql.driver=com.mysql.jdbc.Driver
          mysql.maxCounts=5

          代碼如下:

          /*
          * @(#)JarFileReader.java Oct 9, 2008
          *
          * Copyright (c) 2008 by jadmin. All Rights Reserved.
          */

          package file.jar;

          import java.io.IOException;
          import java.io.InputStream;
          import java.util.Properties;
          import java.util.Set;
          import java.util.jar.JarEntry;
          import java.util.jar.JarFile;

          /**
          * 從jar包中讀取配置文件信息
          *
          * @author <a href="mailto:jadmin@yeah.net">jadmin</a>
          * @version 1.00 Oct 9, 2008 1:10:44 AM
          */
          public class JarReader {

          public static void main(String[] args) {
          ?? String jarPath = "C:\\Documents and Settings\\Administrator\\桌面\\新建文件夾\\kk.jar";
          ?? String entryFile = "config/database.properties";
          ?? doRead(jarPath,entryFile);
          }

          public static void doRead(String jarPath, String entryFile) {
          ?? try {
          ??? JarFile jarFile = new JarFile(jarPath);
          ??? JarEntry entry = jarFile.getJarEntry(entryFile);
          ??? InputStream input = jarFile.getInputStream(entry);
          ??? process(input);
          ??? jarFile.close();
          ?? } catch (IOException e) {
          ??? e.printStackTrace();
          ?? }
          }

          private static void process(InputStream input) {
          ?? Properties p = new Properties();
          ?? try {
          ??? p.load(input);
          ??? Set<?> set = p.keySet();
          ??? for (Object name : set)
          ???? System.out.println(name + "=" + p.getProperty((String) name));
          ?? } catch (IOException e) {
          ??? e.printStackTrace();
          ?? }
          }
          }

          運(yùn)行結(jié)果如下:

          posted on 2008-10-09 01:49 jadmin 閱讀(1740) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 鲁山县| 甘孜| 芜湖市| 五莲县| 定襄县| 双桥区| 田东县| 体育| 晋州市| 塘沽区| 宝清县| 宿州市| 克什克腾旗| 新丰县| 宁明县| 仙居县| 奉化市| 化州市| 许昌县| 临西县| 绵竹市| 浙江省| 洛浦县| 台山市| 本溪| 兴化市| 广东省| 宁陕县| 平泉县| 胶州市| 新巴尔虎右旗| 永清县| 东丰县| 安阳市| 保靖县| 合山市| 郁南县| 普兰店市| 任丘市| 阿瓦提县| 凤庆县|