package test.data.com.utils;
import org.apache.commons.lang3.StringUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
/**
* @param
* @param
* @param
* @author HUAZAI
* @title:
* @description:
* @return 返回类型 :
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 18-10-31 上午10:01
*/
public class UtilsPath {
/**
* @title
* @description 取得当前类所在的文件 包括文件
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 11:09:00
*/
public static Path getClassFilesPath(Class clazz) {
URL path = clazz.getResource(clazz.getName().substring(
clazz.getName().lastIndexOf(".") + 1)
+ ".class");
if (path == null) {
String name = clazz.getName().replaceAll("[.]", "/");
path = clazz.getResource("/" + name + ".class");
}
return Paths.get(path.getPath()).toAbsolutePath();
}
/**
* @title
* @description 同getClassFilesPath 解决中文编码问题
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 11:08:37
*/
public static String getClassFilesPathStr(Class clazz) {
try {
return java.net.URLDecoder.decode(getClassFilesPath(clazz).toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* @title
* @description 取得当前类所在的ClassPath目录
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 11:10:13
*/
public static Path getClassDirPath(Class clazz) {
Path path = getClassFilesPath(clazz);
for (int index = path.getNameCount(); index >= 0 && StringUtils.lastIndexOf(StringUtils.lowerCase(path.toString()), ".jar!") > 0; index--) {
path = path.subpath(0, index);
}
return path;
}
/**
* @title
* @description 同getClassDirPath 解决中文编码问题
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 11:13:11
*/
public static String getClassDirPathStr(Class clazz) {
try {
return java.net.URLDecoder.decode(getClassDirPath(clazz).toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* @title
* @description 获取当前jar包所在的目录
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 10:35:42
*/
public static Path getJarFilePath(Class clazz) {
//该种方式在Windows上获取的路径有问题 获取的路径 类似 /D:/asd/asd 路径前多了一个/
//Path path = Paths.get(clazz.getProtectionDomain().getCodeSource().getLocation().getPath());
String str = clazz.getProtectionDomain().getCodeSource().getLocation().getPath(); logger.debug("str = " + str); File file = new File(str);//使用该方法将/D:/asd/asd 转换为 D:\asd\asd try { logger.debug("file 4 " + file.getCanonicalPath()); Path root = Paths.get(file.getCanonicalPath()).getRoot(); logger.debug("root = " + root.toString()); int indexOf = StringUtils.indexOf(str, root.toString()); logger.debug("indexOf = " + indexOf); str = file.getCanonicalPath(); // str = StringUtils.substring(str, indexOf); logger.debug("getJarFilePath 4 " + file.getCanonicalPath() + " str " + str); } catch (IOException e) { logger.error(e); } Path path = Paths.get(str); logger.debug("Paths.get(str) = " + path); if (!path.isAbsolute()) { path = path.toAbsolutePath(); } logger.debug("getJarFilePath return " + path); return path;
return path.toAbsolutePath();
}
/**
* @title
* @description 获取jar文件路径 解决中文乱码
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 10:37:17
*/
public static String getJarPath(Class clazz) {
try {
return java.net.URLDecoder.decode(getJarFilePath(clazz).toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* @title
* @description 获取jar文件所在文件夹路径 解决中文乱码
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 10:37:17
*/
public static String getJarDirPath(Class clazz) {
try {
return java.net.URLDecoder.decode(getJarFilePath(clazz).getParent().toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* @title
* @description 加载配置文件 配置文件所在文件夹位置是Class的jar包所在的位置
* @author HUAZAI
* @param
* <ul>
* <li></li>
* <li></li>
* <ul>
* @return
* <ul>
* <li></li>
* <li></li>
* <ul>
* @throws
* @date 2018-10-31 14:10:14
*/
public static Properties reloadConfig(Class clazz) {
Properties pro = new Properties();
String jarDirPath = getJarDirPath(clazz);
Path path = Paths.get(jarDirPath, "args.properties");
try (FileInputStream in = new FileInputStream(path.toString())){
pro.load(in);
return pro;
} catch (IOException e) {
return pro;
}
}
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println(getClassFilesPathStr(UtilsPath.class));
System.out.println(getClassDirPathStr(UtilsPath.class));
System.out.println(getJarPath(UtilsPath.class));
System.out.println(getJarDirPath(UtilsPath.class));
System.out.println(System.getProperty("user.dir"));//这种方式不靠谱 这是在哪里执行命令 获取的路径就是哪里
}
}
来源:CSDN
作者:山若万丈-云自萦绕
链接:https://blog.csdn.net/chushoutaizhong/article/details/83583310