QHYun_practice-4

前面做到了程序入口参数的设置,初始化一些全局的常量备用。如果有兴趣也可以自己去扩展一下X64平台的校验,这样你就可以检测当前运行的平台和你期望的常量是不是一致的。

接下来,写一个函数,做一个简单的本地的license校验,不加密的文本校验吧,一个成熟的应用程序应该这样不是嘛,虽然形式比较简单,但是还是走一下比较好。

既然是项目文件的校验,肯定涉及到路径的访问和文件的读取吧,所以干脆先去QHBaseBasic中写两个类吧,这样以后遇到类似的问题就可以直接调用,会方便很多(一个程序肯定涉及到很多的路径访问和文件读取);

先写路径的问题,在之前的QHBasic同级建一个QHHelper的包,写第一个工具类QAbsolutePathHelper。顾名思义,设计这个类的目的就是为了找到项目资源文件相关的路径。

目前我在我的Resouce文件夹下建了两个文件包,一个是Image,以后用来放送到的图片;一个User,用来放一些用户相关的文件,比如license文件我打算放在/User/license/license.txt中。所以希望在工具类中实现一些静态方法,能够帮我直接找到项目工程的路径和这些文件夹的路径。

softPath=System.getProperty("user.dir");
先使用了一下这个方法,发现他刚好能找到qhbase这个目录,也就是Resource的上一级,所以可以在这个基础上进行。

实现代码:

package QHHelper;
 
import java.io.File;
 
/**
 * @author Yeager
 * @date 2023.11.22
 * @version 全局的一些静态常量
 */
 
public class SAbsolutePathHelper {
    /**
     * 软件路径
     */
    private static String softPath=null;
 
    /**
     * 获取项目路径
     * @return
     */
    private static String getSoftPath()
    {
        if(softPath!=null)
            return softPath;
        softPath=System.getProperty("user.dir");
        return softPath;
    }
 
    /**
     * 获取项目文件路径
     * @param
     * @return
     */
    private static String getFilePath(String[] subPaths)
    {
        String path=getSoftPath();
        for(int i=0;i<subPaths.length;i++)
        {
            path+= File.separator;
            path+=subPaths[i];
        }
        return path;
    }
    private static String getFilePath(String subPath)
    {
        return getFilePath(new String[] {subPath});
    }
    /**
     * 获取资源文件路径
     * @param
     * @return
     */
    private static String getResFilePath(String[] subPaths)
    {
        String path=getResourcePath();
        for(int i=0;i<subPaths.length;i++)
        {
            path+= File.separator;
            path+=subPaths[i];
        }
        return path;
    }
    private static String getResFilePath(String subPath)
    {
        return getResFilePath(new String[] {subPath});
    }
    /**
     *  获取Resource文件路径
     */
    public static String getResourcePath()
    {
        return getFilePath("Resource");
    }
    /**
     * 获取资源文件下的 Image路径
     * @param
     * @return
     */
    public static String getImagePath()
    {
        return getResFilePath("Image");
    }
 
    /**
     * 获取资源文件下的 User路径
     * @param
     * @return
     */
    public static String getUserPath()
    {
        return getResFilePath("User");
    }
}

分别访问三个函数后的返回结果

 

posted @ 2025-07-21 09:20  Wind_Swing_Dunn  阅读(5)  评论(0)    收藏  举报