Skip to content

Commit

Permalink
支持Java 8 API
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeBottle committed Aug 15, 2024
1 parent 45520c2 commit 8510d83
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
public class LoggerFormatter extends Formatter {
private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]");

// TODO:待测试:Logger 格式化不输出中文字符
@Override
public String format(LogRecord record) {
StringBuilder sb = new StringBuilder("[PotatoPeeler] ");
sb.append(dateFormatter.format(new Date(record.getMillis())))
.append(" [")
.append(record.getLevel().getLocalizedName())
.append(record.getLevel().getName())
.append("] ")
.append(record.getMessage());
if (record.getThrown() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -44,7 +45,8 @@ public class ArgsUtils {
* @throws IOException 文件读取失败时抛出;文件没有提供任何参数时也会抛出
*/
public static String[] readArgsFromFile(String filePath) throws IOException {
Path argFilePath = Path.of(filePath);
// TODO: 待测试是否能正常工作
Path argFilePath = Paths.get(filePath);
byte[] allBytes = Files.readAllBytes(argFilePath);
String[] argList = new String(allBytes).split("\\s+");
// 去除头部的空字串
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@

import indi.somebottle.logger.GlobalLogger;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* 此类用于记录 Peeler 程序上次的运行时间,防止每次启动都执行一次
*/

public class TimeUtils {
// 记录上次运行时间的文件
private static final File timeRecordFile = new File("./peeler.lrt");
// 记录上次运行时间的文件的路径
private static final Path timeRecordFilePath = Paths.get("./peeler.lrt");
// 上次运行的时间
private static long lastRunTime = 0;

// TODO: 测试在 Java 8 API 下是否能正常工作
static {
if (timeRecordFile.exists()) {
if (Files.exists(timeRecordFilePath)) {
try {
// 读取上次运行的时间
lastRunTime = Long.parseLong(Files.readString(timeRecordFile.toPath()));
// 支持 Java 8 API
lastRunTime = Long.parseLong(new String(Files.readAllBytes(timeRecordFilePath)));
} catch (IOException e) {
GlobalLogger.severe("Failed to read last run time from file: " + timeRecordFile.getAbsolutePath(), e);
GlobalLogger.severe("Failed to read last run time from file: " + timeRecordFilePath, e);
System.exit(1);
}
}
Expand All @@ -44,9 +47,10 @@ public static long timeNow() {
public static void setLastRunTime(long lastRunTime) {
TimeUtils.lastRunTime = lastRunTime;
try {
Files.writeString(timeRecordFile.toPath(), String.valueOf(lastRunTime));
// 支持 Java 8 API
Files.write(timeRecordFilePath, Long.toString(lastRunTime).getBytes());
} catch (IOException e) {
GlobalLogger.severe("Failed to write last run time to file: " + timeRecordFile.getAbsolutePath(), e);
GlobalLogger.severe("Failed to write last run time to file: " + timeRecordFilePath, e);
}
}
}

0 comments on commit 8510d83

Please sign in to comment.