Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
勤为径苦作舟
ZUtil
提交
a54335aa
Z
ZUtil
项目概览
勤为径苦作舟
/
ZUtil
10 个月 前同步成功
通知
6
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Z
ZUtil
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a54335aa
编写于
10月 28, 2022
作者:
勤为径苦作舟
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: FileUtils.getClassRootPath 重命名为 getResourcePath,返回“file:/”开头的路径时不截取
上级
53c853e0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
27 addition
and
14 deletion
+27
-14
docs/docs/999-incompatible-changes.md
docs/docs/999-incompatible-changes.md
+2
-0
src/main/java/top/zhogjianhao/FileUtils.java
src/main/java/top/zhogjianhao/FileUtils.java
+20
-9
src/test/java/top/zhogjianhao/junit/FileUtilsTest.java
src/test/java/top/zhogjianhao/junit/FileUtilsTest.java
+5
-5
未找到文件。
docs/docs/999-incompatible-changes.md
浏览文件 @
a54335aa
# 不兼容变更
*
1.9.4:
*
`FileUtils.getClassRootPath`
重命名为
`getResourcePath`
。
*
1.8.1:
*
`date.DateFeature`
中静态变量的赋值方式由直接赋值改为通过
`set`
、
`setAlways`
方法赋值;
*
`DateUtils.defaultLocalDatePattern`
移至
`DateConstant.DEFAULT_LOCAL_DATE_PATTERN`
;
...
...
src/main/java/top/zhogjianhao/FileUtils.java
浏览文件 @
a54335aa
...
...
@@ -7,7 +7,6 @@ import top.zhogjianhao.regex.RegExUtils;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.Objects
;
import
java.util.regex.Pattern
;
@Slf4j
...
...
@@ -55,9 +54,13 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
* @param clazz 类
* @return 类的根路径(不含包)
*/
public
static
String
getClassRootPath
(
@NonNull
final
Class
<?>
clazz
)
{
if
(
SystemUtils
.
IS_OS_WINDOWS
){
return
clazz
.
getClassLoader
().
getResource
(
""
).
getPath
().
substring
(
1
);
public
static
String
getResourcePath
(
@NonNull
final
Class
<?>
clazz
)
{
if
(
SystemUtils
.
IS_OS_WINDOWS
)
{
String
result
=
clazz
.
getClassLoader
().
getResource
(
""
).
getPath
();
if
(
result
.
startsWith
(
"/"
))
{
result
=
result
.
substring
(
1
);
}
return
result
;
}
return
clazz
.
getClassLoader
().
getResource
(
""
).
getPath
();
}
...
...
@@ -67,9 +70,13 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
*
* @return 类的根路径(不含包)
*/
public
static
String
getClassRootPath
()
{
if
(
SystemUtils
.
IS_OS_WINDOWS
){
return
Thread
.
currentThread
().
getContextClassLoader
().
getResource
(
""
).
getPath
().
substring
(
1
);
public
static
String
getResourcePath
()
{
if
(
SystemUtils
.
IS_OS_WINDOWS
)
{
String
result
=
Thread
.
currentThread
().
getContextClassLoader
().
getResource
(
""
).
getPath
();
if
(
result
.
startsWith
(
"/"
))
{
result
=
result
.
substring
(
1
);
}
return
result
;
}
return
Thread
.
currentThread
().
getContextClassLoader
().
getResource
(
""
).
getPath
();
}
...
...
@@ -81,8 +88,12 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
* @return 类路径
*/
public
static
String
getClassPath
(
@NonNull
final
Class
<?>
clazz
)
{
if
(
SystemUtils
.
IS_OS_WINDOWS
){
return
clazz
.
getResource
(
""
).
getPath
().
substring
(
1
);
if
(
SystemUtils
.
IS_OS_WINDOWS
)
{
String
result
=
clazz
.
getResource
(
""
).
getPath
();
if
(
result
.
startsWith
(
"/"
))
{
result
=
result
.
substring
(
1
);
}
return
result
;
}
return
clazz
.
getResource
(
""
).
getPath
();
}
...
...
src/test/java/top/zhogjianhao/junit/FileUtilsTest.java
浏览文件 @
a54335aa
...
...
@@ -15,15 +15,15 @@ public class FileUtilsTest {
private
static
final
String
PROJECT_PATH_SLASH
=
FileUtils
.
getUserDir
().
replace
(
"\\"
,
"/"
);
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
FileUtils
.
get
ClassRoot
Path
());
System
.
out
.
println
(
FileUtils
.
get
ClassRoot
Path
(
FileUtilsTest
.
class
));
System
.
out
.
println
(
FileUtils
.
get
Resource
Path
());
System
.
out
.
println
(
FileUtils
.
get
Resource
Path
(
FileUtilsTest
.
class
));
System
.
out
.
println
(
FileUtils
.
getClassPath
(
FileUtilsTest
.
class
));
}
@DisplayName
(
"getPath:获取各种路径"
)
@Test
void
getPath
()
{
assertThrows
(
NullPointerException
.
class
,
()
->
FileUtils
.
get
ClassRoot
Path
(
null
));
assertThrows
(
NullPointerException
.
class
,
()
->
FileUtils
.
get
Resource
Path
(
null
));
assertThrows
(
NullPointerException
.
class
,
()
->
FileUtils
.
getClassPath
(
null
));
assertThrows
(
NullPointerException
.
class
,
()
->
FileUtils
.
getDirPathAndNameByPath
(
null
));
assertThrows
(
NullPointerException
.
class
,
()
->
FileUtils
.
getDirPathByPath
(
null
));
...
...
@@ -31,8 +31,8 @@ public class FileUtilsTest {
assertEquals
(
PROJECT_PATH
,
FileUtils
.
getUserDir
());
assertEquals
(
PROJECT_PATH
,
FileUtils
.
getProjectPath
());
assertEquals
(
PROJECT_PATH_SLASH
+
"/target/test-classes/"
,
FileUtils
.
get
ClassRoot
Path
());
assertEquals
(
PROJECT_PATH_SLASH
+
"/target/test-classes/"
,
FileUtils
.
get
ClassRoot
Path
(
FileUtilsTest
.
class
));
assertEquals
(
PROJECT_PATH_SLASH
+
"/target/test-classes/"
,
FileUtils
.
get
Resource
Path
());
assertEquals
(
PROJECT_PATH_SLASH
+
"/target/test-classes/"
,
FileUtils
.
get
Resource
Path
(
FileUtilsTest
.
class
));
assertEquals
(
PROJECT_PATH_SLASH
+
"/target/test-classes/top/zhogjianhao/junit/"
,
FileUtils
.
getClassPath
(
FileUtilsTest
.
class
));
assertEquals
(
"1.txt"
,
FileUtils
.
getDirPathAndNameByPath
(
"1.txt"
)[
1
]);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录