mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
commit
135b172653
@ -3578,4 +3578,21 @@ public class FileUtil {
|
||||
public static void tail(File file, Charset charset) {
|
||||
FileUtil.tail(file, charset, Tailer.CONSOLE_HANDLER);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取class类当前文件夹, 不管是否在jar包中都会返回文件夹的路径
|
||||
* class在jar包中返回jar所在文件夹,class不在jar中返回文件夹目录
|
||||
* jdk中的类不能使用此方法
|
||||
*/
|
||||
public static String getClassDir(Class clazz) {
|
||||
String currentDir = null;
|
||||
File file = new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath());
|
||||
if (file.isFile()) {
|
||||
currentDir = file.getParentFile().getAbsolutePath();
|
||||
} else {
|
||||
currentDir = file.getAbsolutePath();
|
||||
}
|
||||
return currentDir;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package cn.hutool.core.io.file;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TailerTest {
|
||||
|
||||
@ -20,4 +20,12 @@ public class TailerTest {
|
||||
Tailer tailer = new Tailer(FileUtil.file("f:/test/test.log"), Tailer.CONSOLE_HANDLER, 2);
|
||||
tailer.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testDir() {
|
||||
System.out.println(FileUtil.getClassDir(FileUtil.class));
|
||||
System.out.println(FileUtil.getClassDir(Data.class));
|
||||
System.out.println(FileUtil.getClassDir(Test.class));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user