分享我的两个工具类

分享我的两个工具类

  • 新增 LjfUtils 类,提供操作系统判断方法
  • 新增 PathUtils 类,实现路径获取和文件读取功能
public class LjfUtils {

    /**
     * 判断是否windows系统
     * @return
     */
    public static boolean isRunInWindows(){
        return System.getProperty("os.name").toLowerCase().startsWith("win");
    }

    /**
     * 判断否是linux系统
     * @return
     */
    public static boolean isRunInLinux(){
        return System.getProperty("os.name").toLowerCase().startsWith("linux");
    }

    /**
     * 判断是否运行在IDEA
     * @return
     */
    public static boolean isRunInIDEA() {
        try {
            Class.forName("com.intellij.rt.execution.application.AppMainV2");
            return true;
        } catch (ClassNotFoundException ignored) {
            return false;
        }
    }
}
public class PathUtils {

    /**
     * 获取当前应用jar包所在路径
     *
     * @return
     */
    public static String getJarPath() {

        String path = PathUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        int begIndex = 0;
        int endIndex = 0;
        if (LjfUtils.isRunInIDEA()) {
            path = path.substring(1);
            endIndex = path.lastIndexOf("/");
        } else if (LjfUtils.isRunInLinux()) {
            begIndex = 5;
            endIndex = path.indexOf("!");
            path = path.substring(0, endIndex);
            endIndex = path.lastIndexOf("/");
        } else if (LjfUtils.isRunInWindows()) {
            begIndex = 6;
            endIndex = path.indexOf("!");
            path = path.substring(0, endIndex);
            endIndex = path.lastIndexOf("/");
        }

        path = path.substring(begIndex, endIndex)+"/";
        try {
            path = java.net.URLDecoder.decode(path, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return path;
    }

    /**
     * 获取resource下的文件
     *
     * @param fileName 文件名
     */
    public static void getResource(String fileName) {
        InputStream in = PathUtils.class.getClassLoader().getResourceAsStream(fileName);
        printFileContent(in);
    }

    /**
     * 打印文件内容
     *
     * @param obj
     */
    public static void printFileContent(Object obj) {
        if (null == obj) {
            throw new RuntimeException("参数为空");
        }
        BufferedReader reader = null;
        try {
            // 如果是文件路径
            if (obj instanceof String) {
                reader = new BufferedReader(new FileReader(new File((String) obj)));
                // 如果是文件输入流
            } else if (obj instanceof InputStream) {
                reader = new BufferedReader(new InputStreamReader((InputStream) obj));
            }
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

one one day

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值