获得不同操作系统的classes目录 如windows和linux的classes

获得不同操作系统的classes目录 如windows和linux的classes

因为项目需要读取classes下面的资源文件

为了方便在不同的操作系统中运行,如windows和linux下都能运行,这里的运行是指的都能在不更改源代码的情况下自动获取(或少量修改)


以下为工具类InitVar的代码实现

public class InitVar {
 public static String classesPath;
 
 //获得操作系统的名字
 public static String OS = System.getProperty("os.name");
 
 static{
 //获得路径
 classesPath = getClassesPath();
 System.out.println(1111);
 }
 
 
 //以下两个方法为判断是否是linux或windows系统
 private static boolean isLinux(){  
        return OS.toLowerCase().indexOf("linux")>=0;  
    } 
 
 private static boolean isWindows(){  
        return OS.toLowerCase().indexOf("windows")>=0;  
    } 
 //根据不同的结果返回不同的路径
 private static String getClassesPath(){
 System.out.println(2222);
 if(isLinux()){
 return (Thread.currentThread().getContextClassLoader().getResource("").toString()).replace("%20", " ").replace("file:", "");
 }
 if(isWindows()){
 return (Thread.currentThread().getContextClassLoader().getResource("").toString()).substring(1).replace("%20", " ").replace("file:", "");
 }
 return "请检查操作系统:不是windows或linux";
 }

比如我们要加载classes目录下的 config_webinfo.properties文件,部分代码如下:

Properties prop = new Properties();
//获得配置文件 
InputStream in;
try {
	in = new BufferedInputStream(new FileInputStream(InitVar.classesPath+"config_webinfo.properties"));
	prop.load(in);
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}


如果我们的配置文件在resources文件夹下initweb文件夹内,那么处理就有点小改动

windows的代码片段是:

Properties prop = new Properties();
//获得配置文件 
InputStream in;
try {
	in = new BufferedInputStream(new FileInputStream(InitVar.classesPath+"resources\\initweb\\config_webinfo.properties"));
	prop.load(in);
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
linux的代码片段是:



Properties prop = new Properties();
//获得配置文件 
InputStream in;
try {
	in = new BufferedInputStream(new FileInputStream(InitVar.classesPath+"resources/initweb/config_webinfo.properties"));
	prop.load(in);
} catch (FileNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
其实这个问题也好解决,在工具类InitVar增加两个方法,这个方法有一个参数,参数就是  InitVar.classesPath+"resources/initweb/config_webinfo.properties")

无需关心 加号后面的路径是windows的路径还是linux的路径

然后由这个方法自动将正斜杠或反斜杠进行转换

两个方法说明:

getClasses()  ---->  相当于直接获得classespath

getClasses(String dir)  ----> 参数表示配置文件在classes的其他目录,dir的写法可以是windows的反斜杠,也可以是linux的正斜杠

方法实现:

/*
 * 如果无参,就表示获取classes的根目录
 * 此时获得到的是根目录,而不是配置文件的详细地址
 */
public String getClasses(){
	return classesPath;
}
/*
 * 如果有参,就表示配置文件不在classes的根目录,而是在二级或三级目录下
 * 就需要稍微修改下
 * 此时获取到的是配置文件的详细地址
 * 我们无需关心dir究竟是windows的格式还是linux格式
 */
public String getClasses(String dir){
	if(isLinux()){
		return classesPath+dir.replace("\\", "/");
	}
	if(isWindows()){
		return classesPath+dir.replace("/", "\\");
	}
	return "请检查操作系统:不是windows或linux";
}
然后您就可以根据自己的需求写了,比如您的配置文件在classes的根目录,您就可以通过两种方式获得classes的根目录

一种是直接获取静态变量classesPath,另外一种是调用方法getClasses()方法

如果您的配置文件在classes的其他目录,你就可以直接调用方法 getClasses(String dir)方法来获取配置文件的全路径


爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情Blog Img