Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
T
test-data-manalysis
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Members
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
朱广松
test-data-manalysis
Commits
0916374e
Commit
0916374e
authored
Sep 19, 2025
by
zhugs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor:代码优化
parent
16478d06
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
60 行增加
和
51 行删除
+60
-51
src/main/java/Main.java
+9
-9
src/main/java/text/TextHandler.java
+10
-40
src/main/java/util/ZipUtil.java
+34
-0
src/main/java/watcher/DirWatcher.java
+7
-2
没有找到文件。
src/main/java/Main.java
View file @
0916374e
import
database.InsertData
;
import
execl.ExcelHandlerFactory
;
import
text.TextHandlerFactory
;
import
lombok.extern.slf4j.Slf4j
;
import
util.ConfigUtil
;
...
...
@@ -12,6 +13,7 @@ import java.util.function.Predicate;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
// 监听 text log文件
Path
dirText
=
Path
.
of
(
ConfigUtil
.
getWatchDir
());
// 设置要匹配的文件规则
Predicate
<
File
>
textFilter
=
file
->
{
...
...
@@ -25,19 +27,17 @@ public class Main {
}
return
true
;
};
new
DirWatcher
(
dirText
,
textFilter
,
new
TextHandlerFactory
()).
startWatch
();
// 监听 excel log文件
// File dirExcel = new File("C:\\Users\\zhugso\\Projects\\chenyang\\testdata\\exc");
// Predicate<File> excelFilter = file -> {
// String name = file.getName();
// return !name.contains("$");
// };
//
// ExcelFileWatcher excelWatcher = new ExcelFileWatcher(dirExcel, excelFilter);
// excelWatcher.startWatch();
Path
dirExcel
=
Path
.
of
(
"C:\\Users\\zhugso\\Projects\\chenyang\\testdata\\exc"
);
Predicate
<
File
>
excelFilter
=
file
->
{
String
name
=
file
.
getName
();
return
!
name
.
contains
(
"$"
);
};
new
DirWatcher
(
dirExcel
,
excelFilter
,
new
ExcelHandlerFactory
()).
startWatch
();
// 阻塞主线程
log
.
info
(
"程序已启动"
);
...
...
src/main/java/text/TextHandler.java
View file @
0916374e
...
...
@@ -4,8 +4,12 @@ import database.InsertData;
import
domain.ResultData
;
import
handler.AbstractFileHandler
;
import
lombok.extern.slf4j.Slf4j
;
import
util.ZipUtil
;
import
java.io.*
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.text.DateFormat
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
...
...
@@ -14,8 +18,6 @@ import java.util.Date;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
@Slf4j
public
class
TextHandler
extends
AbstractFileHandler
{
...
...
@@ -55,7 +57,7 @@ public class TextHandler extends AbstractFileHandler {
}
// 目标数据匹配
ResultData
data
=
S
tringParser
(
patternText
);
ResultData
data
=
s
tringParser
(
patternText
);
data
.
setFileName
(
file
.
getName
());
String
[]
ss
=
file
.
getName
().
split
(
"_"
);
...
...
@@ -72,8 +74,9 @@ public class TextHandler extends AbstractFileHandler {
data
.
setType
(
"Wifi"
);
// 压缩日志文件并删除
String
zipName
=
zipLogFile
(
file
);
// 压缩日志文件
String
zipName
=
ZipUtil
.
zipFile
(
file
);
log
.
info
(
"文件是否删除: {}, {}"
,
file
.
delete
(),
file
.
getName
());
data
.
setZipFileUrl
(
zipName
);
data
.
setFileUrl
(
file
.
getParent
()
+
zipName
);
...
...
@@ -81,7 +84,7 @@ public class TextHandler extends AbstractFileHandler {
new
InsertData
().
insert
(
data
);
}
private
ResultData
S
tringParser
(
List
<
String
>
patternText
)
{
private
ResultData
s
tringParser
(
List
<
String
>
patternText
)
{
String
regex
=
"\\[PASS] (?<itemName>.+?) (?<unit>\\w+), value = \"(?<value>[-\\d.]+)\" (?<range>\\([^)]*\\))"
;
Pattern
pattern
=
Pattern
.
compile
(
regex
);
ResultData
data
=
new
ResultData
();
...
...
@@ -109,37 +112,4 @@ public class TextHandler extends AbstractFileHandler {
return
data
;
}
private
String
zipLogFile
(
File
file
)
{
String
zipFile
=
file
.
getParent
()
+
File
.
separator
+
getBaseName
(
file
.
getName
())
+
".zip"
;
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
zipFile
);
ZipOutputStream
zos
=
new
ZipOutputStream
(
fos
))
{
try
(
FileInputStream
fis
=
new
FileInputStream
(
file
))
{
// 创建 ZipEntry,名字就是压缩包里的文件名
ZipEntry
zipEntry
=
new
ZipEntry
(
file
.
getName
());
zos
.
putNextEntry
(
zipEntry
);
byte
[]
buffer
=
new
byte
[
1024
];
int
length
;
while
((
length
=
fis
.
read
(
buffer
))
>=
0
)
{
zos
.
write
(
buffer
,
0
,
length
);
}
}
log
.
info
(
"压缩完成:{}"
,
zipFile
);
log
.
info
(
"文件是否删除: {}, {}"
,
file
.
delete
(),
file
.
getName
());
}
catch
(
IOException
e
)
{
log
.
error
(
e
.
getMessage
());
}
return
zipFile
;
}
private
String
getBaseName
(
String
fileName
)
{
int
dotIndex
=
fileName
.
lastIndexOf
(
'.'
);
return
(
dotIndex
==
-
1
)
?
fileName
:
fileName
.
substring
(
0
,
dotIndex
);
}
}
src/main/java/util/ZipUtil.java
View file @
0916374e
package
util
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.*
;
import
java.util.zip.Deflater
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipInputStream
;
import
java.util.zip.ZipOutputStream
;
@Slf4j
public
class
ZipUtil
{
private
ZipUtil
()
{
}
...
...
@@ -47,6 +52,35 @@ public class ZipUtil {
return
null
;
}
public
static
String
zipFile
(
File
file
)
{
String
zipFile
=
file
.
getParent
()
+
File
.
separator
+
getBaseName
(
file
.
getName
())
+
".zip"
;
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
zipFile
);
ZipOutputStream
zos
=
new
ZipOutputStream
(
fos
))
{
try
(
FileInputStream
fis
=
new
FileInputStream
(
file
))
{
// 创建 ZipEntry,名字就是压缩包里的文件名
ZipEntry
zipEntry
=
new
ZipEntry
(
file
.
getName
());
zos
.
setLevel
(
Deflater
.
BEST_COMPRESSION
);
zos
.
putNextEntry
(
zipEntry
);
byte
[]
buffer
=
new
byte
[
1024
];
int
length
;
while
((
length
=
fis
.
read
(
buffer
))
>=
0
)
{
zos
.
write
(
buffer
,
0
,
length
);
}
}
log
.
info
(
"压缩完成:{}"
,
zipFile
);
}
catch
(
IOException
e
)
{
log
.
error
(
e
.
getMessage
());
}
return
zipFile
;
}
private
static
String
getBaseName
(
String
fileName
)
{
int
dotIndex
=
fileName
.
lastIndexOf
(
'.'
);
return
(
dotIndex
==
-
1
)
?
fileName
:
fileName
.
substring
(
0
,
dotIndex
);
}
}
src/main/java/watcher/DirWatcher.java
View file @
0916374e
...
...
@@ -44,12 +44,17 @@ public class DirWatcher {
for
(
WatchEvent
<?>
event
:
key
.
pollEvents
())
{
Path
fullPath
=
dir
.
resolve
((
Path
)
event
.
context
());
File
file
=
fullPath
.
toFile
();
if
(!
fileFilter
.
test
(
file
))
{
continue
;
}
if
(!
waitForFileReady
(
file
,
3
,
500
))
{
log
.
error
(
"文件打开失败,已跳过:{}"
,
file
.
getName
());
continue
;
}
if
(
fileFilter
.
test
(
file
)
&&
waitForFileReady
(
file
,
3
,
500
))
{
log
.
info
(
"文件已创建: {}"
,
file
.
getName
());
Thread
.
ofVirtual
().
start
(
fileHandler
.
create
(
file
));
}
}
key
.
reset
();
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论