添加 临时目录相关方法
This commit is contained in:
1 parent
03e223df9b
commit
441891652c
5 files changed
+119
-8
No files matched your search
@@ -2,6 +2,8 @@ package top.gtb520.java.gnu.java.utils.library.utils.runtime;
|
||||
|
||||
import top.gtb520.java.gnu.java.utils.library.main;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
// 获取运行时的信息
|
||||
public class RunEnv {
|
||||
// 获取当前运行的JAR文件路径
|
||||
@@ -9,9 +11,42 @@ public class RunEnv {
|
||||
String path = main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
|
||||
if (System.getProperty("os.name").startsWith("Windows")) {
|
||||
path = path.replace('/', '\\');
|
||||
//去除路径最前面的 “\”
|
||||
//去除路径最前面的 "\\"
|
||||
path = path.substring(1);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
// 获取临时目录
|
||||
public static String getTempDir() {
|
||||
return System.getProperty("java.io.tmpdir");
|
||||
}
|
||||
|
||||
// 新建临时目录,失败时返回 null
|
||||
public static String newTempDir() {
|
||||
return createTempDir(Path.of(getTempDir()));
|
||||
}
|
||||
|
||||
// 在 当前运行的JAR文件路径 下 新建临时目录,失败时返回 null
|
||||
public static String newJarThisTempDir() {
|
||||
Path parent = Path.of(getJarPath()).getParent();
|
||||
if (parent == null) {
|
||||
return null;
|
||||
}
|
||||
return createTempDir(parent);
|
||||
}
|
||||
|
||||
// 在指定目录下创建以时间戳命名的临时目录,返回目录路径;目录已存在时返回 null
|
||||
private static String createTempDir(Path parent) {
|
||||
Path tempDir = parent.resolve(String.valueOf(System.currentTimeMillis()));
|
||||
if (!tempDir.toFile().exists()) {
|
||||
try {
|
||||
tempDir.toFile().mkdirs();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return tempDir.toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package top.gtb520.java.gnu.java.utils.library;
|
||||
|
||||
public class version {
|
||||
// 项目的版本信息,不代表实时运行环境
|
||||
public static final String VERSION = "1.2";
|
||||
public static final String VERSION = "1.3";
|
||||
public static final String JAVA_VERSION = "25";
|
||||
public static final String OpenJDK_VERSION = "25.0.0.0.1";
|
||||
public static final String OpenJDK_Runtime_Environment = "25.0.0.0.1+36-GA";
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package top.gtb520.java.gnu.java.utils.library.utils.runtime;
|
||||
|
||||
import static top.gtb520.java.gnu.java.utils.library.utils.runtime.RunEnv.getJarPath;
|
||||
import static top.gtb520.java.gnu.java.utils.library.utils.runtime.RunEnv.*;
|
||||
|
||||
class RunEnvTest {
|
||||
public static void main() {
|
||||
System.out.println(getJarPath());
|
||||
System.out.printf(newJarThisTempDir() + "\n");
|
||||
System.out.println(newTempDir());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user