总结:利用原生JDK封装工具类,解析properties配置文件以及MF清单文件

总结:利用原生JDK封装工具类,解析properties配置文件以及MF清单文件

一·背景描述:

1.在不同的项目中,项目使用的开发框架都不一样,甚至是JDK原生开发模式。此时解析配置文件以及jar包中的清单文件,就只能利用JDK原生办法解析,而无法利用流行热门框架解析

2.了解JDK原生解析配置文件以及清单文件的实现方式,有助于自己理解各种开源框架的底层实现方式。因为大多数开源框架本质就是在JDK、JavaEE的基础上,利用各种设计模式进行封装而来,从而提升程序员的开发效率。

二·properties配置文件解析工具类:该工具类也可以用于解析MANIFEST.MF清单文件

java 复制代码
package utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * @Description Properties解析工具类,manifest.mf文件工具类
 * 注意:一个key后面的值不能换行,否则会读取不完整
 * <p>
 * Copyright @ 2022 Shanghai Mise Co. Ltd.
 * All right reserved.
 * <p>
 * @Author LiuMingFu
 * @Date 2023-05-17 16:25
 */
public class PropUtil {
    public static void main(String[] args) {
        String fileUrl = "/Users/ideal/Desktop/web/test/shgc_WeChat2OA_config.properties";
        String keySecret = getPropValue(fileUrl, "key_secret");
        System.out.println(keySecret);
        File file = new File(fileUrl);
        String port = getPropValue(file, "port");
        System.out.println(port);
    }

    /**
     * 解析配置文件/manifest.mf文件,获取指定key的value值
     *
     * @param filePath Properties配置文件路径/manifest.mf文件路径
     * @param key      需要获取值的key
     * @return
     * @author LiuMingFu
     * @date 2023-05-17
     */
    public static String getPropValue(String filePath, String key) {
        String value = "";
        try {
            FileInputStream fileInput = new FileInputStream(filePath);
            Properties prop = new Properties();
            prop.load(fileInput);
            value = prop.getProperty(key);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return value;
    }


    /**
     * 解析配置文件/manifest.mf文件,获取指定key的value值
     *
     * @param file Properties配置文件对象/manifest.mf文件对象
     * @param key  需要获取值的key
     * @return
     * @author LiuMingFu
     * @date 2023-05-17
     */
    public static String getPropValue(File file, String key) {
        String value = "";
        try {
            FileInputStream fileInput = new FileInputStream(file);
            Properties prop = new Properties();
            prop.load(fileInput);
            value = prop.getProperty(key);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return value;
    }

}

三·MANIFEST.MF清单文件解析工具类:

java 复制代码
package utils;


import java.io.IOException;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import static java.util.jar.Attributes.Name.MAIN_CLASS;


/**
 * @Description TODO
 * <p>
 * Copyright @ 2022 Shanghai Mise Co. Ltd.
 * All right reserved.
 * <p>
 * @Author LiuMingFu
 * @Date 2023/11/9 10:52
 */
public class JarUtil {
    public static void main(String[] args) throws IOException {
        String filePath = "/Users/ideal/Desktop/lib/Hello2.jar";
        Attributes attributes = parseManifestFile(filePath);
        for (Object o : attributes.keySet()) {
            System.out.println("key=" + o + "  value=" + attributes.getValue(o.toString()));
        }

        System.out.println(getMainClass(filePath));
        System.out.println(getMFValue(filePath, "Created-By"));
    }


    /**
     * 解析jar包中的MANIFEST.MF(manifest.mf)文件,返回一个类似map的集合对象
     *
     * @param jarFilePath jar包路径
     * @return
     * @author LiuMingFu
     * @date 2023/11/9
     */
    public static Attributes parseManifestFile(String jarFilePath) throws IOException {
        if (jarFilePath != null && !"".equals(jarFilePath)) {
            JarFile jarFile = new JarFile(jarFilePath);
            Manifest manifest = jarFile.getManifest();
            Attributes mainAttributes = manifest.getMainAttributes();
            return mainAttributes;
        } else {
            return null;
        }
    }


    /**
     * 解析jar包中的MANIFEST.MF(manifest.mf)文件,返回一个Main-Class的属性值
     *
     * @param jarFilePath jar包路径
     * @return
     * @author LiuMingFu
     * @date 2023/11/9
     */
    public static String getMainClass(String jarFilePath) throws IOException {
        JarFile jarFile = new JarFile(jarFilePath);
        Manifest mf = jarFile.getManifest();
        Attributes mainAttributes = mf.getMainAttributes();
        for (Map.Entry<Object, Object> entry : mainAttributes.entrySet()) {
            String key = entry.getKey().toString();
            if (key.equalsIgnoreCase(MAIN_CLASS.toString())) {
                return entry.getValue().toString();
            }
        }
        return "";
    }


    /**
     * 解析jar包中的MANIFEST.MF(manifest.mf)文件,返回一个指定key的属性值
     *
     * @param jarFilePath jar包路径
     * @param key         manifest.mf文件的一个key
     * @return
     * @author LiuMingFu
     * @date 2023/11/9
     */
    public static String getMFValue(String jarFilePath, String key) throws IOException {
        JarFile jarFile = new JarFile(jarFilePath);
        Manifest mf = jarFile.getManifest();
        Attributes mainAttributes = mf.getMainAttributes();
        for (Map.Entry<Object, Object> entry : mainAttributes.entrySet()) {
            String keyTemp = entry.getKey().toString();
            if (keyTemp.equalsIgnoreCase(key)) {
                return entry.getValue().toString();
            }
        }
        return "";
    }
}
相关推荐
利刃大大4 分钟前
【SpringBoot】Spring IOC && DI && 五大注解 && Bean && 扫描路径 && 依赖注入
java·spring boot·spring
William_cl5 分钟前
【CSDN 精品专栏】ASP.NET Razor 变量输出 @变量名:从入门到避坑,新手也能写对!
java·数据库·asp.net
尤物程序猿24 分钟前
spring的监听器的几种使用方式
java·数据库·spring
老华带你飞25 分钟前
学生请假管理|基于springboot 学生请假管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·spring
毕设源码-钟学长29 分钟前
【开题答辩全过程】以 基于java的点餐猫在线个性化点餐系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
一 乐44 分钟前
校务管理|基于springboot + vueOA校务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring
摇滚侠1 小时前
面试实战 问题三十四 对称加密 和 非对称加密 spring 拦截器 spring 过滤器
java·spring·面试
xqqxqxxq1 小时前
Java 集合框架之线性表(List)实现技术笔记
java·笔记·python
L0CK1 小时前
RESTful风格解析
java