Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
F
fp-acq-wz
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Matrix
fp-acq-wz
Commits
9f4a2517
提交
9f4a2517
authored
7月 28, 2020
作者:
Your Name
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[fix] FileCreator工具类补上
上级
bc6a4051
流水线
#197
已取消 于阶段
变更
1
流水线
1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
149 行增加
和
0 行删除
+149
-0
FileCreator.java
...src/main/java/com/zjty/fp/acq/misc/utils/FileCreator.java
+149
-0
没有找到文件。
acq-misc/src/main/java/com/zjty/fp/acq/misc/utils/FileCreator.java
0 → 100644
浏览文件 @
9f4a2517
package
com
.
zjty
.
fp
.
acq
.
misc
.
utils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.stereotype.Component
;
import
java.io.File
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.List
;
@Slf4j
@Scope
(
"prototype"
)
@Component
public
class
FileCreator
{
public
File
createFile
(
String
subName
,
String
catalog
,
String
data
)
{
String
pathname
=
"files/"
+
subName
+
"/"
+
catalog
+
"/"
;
String
filename
=
getFileNameByDate
(
subName
,
catalog
);
Path
path
=
Paths
.
get
(
pathname
+
filename
);
boolean
make
=
createFilePath
(
new
File
(
pathname
));
if
(
make
)
{
try
{
Files
.
write
(
path
,
data
.
getBytes
());
}
catch
(
IOException
e
)
{
log
.
info
(
"生成文件时出现异常:"
+
e
);
}
}
return
new
File
(
pathname
+
filename
);
}
public
File
createFileNoTime
(
String
subName
,
String
catalog
,
String
data
)
{
String
pathname
=
"files/"
+
subName
+
"/"
+
catalog
+
"/"
;
String
filename
=
subName
+
"-"
+
catalog
+
".txt"
;
Path
path
=
Paths
.
get
(
pathname
+
filename
);
boolean
make
=
createFilePath
(
new
File
(
pathname
));
if
(
make
)
{
try
{
Files
.
write
(
path
,
data
.
getBytes
());
}
catch
(
IOException
e
)
{
log
.
info
(
"生成文件时出现异常:"
+
e
);
}
}
return
new
File
(
pathname
+
filename
);
}
public
File
createFileInUTF
(
String
subName
,
String
catalog
,
Long
index
,
List
<
String
>
list
){
String
pathname
=
"files/"
+
subName
+
"/"
+
catalog
+
"/"
;
String
filename
=
subName
+
"-"
+
catalog
+
"-"
+
index
+
".txt"
;
Path
path
=
Paths
.
get
(
pathname
+
filename
);
boolean
make
=
createFilePath
(
new
File
(
pathname
));
if
(
make
)
{
try
{
Files
.
write
(
path
,
list
);
}
catch
(
IOException
e
)
{
log
.
info
(
"生成文件时出现异常:{}"
+
e
);
}
}
return
new
File
(
pathname
+
filename
);
}
public
File
createFile
(
String
subName
,
String
catalog
,
List
<
String
>
list
)
{
String
pathname
=
"files/"
+
subName
+
"/"
+
catalog
+
"/"
;
String
filename
=
getFileNameByDate
(
subName
,
catalog
);
Path
path
=
Paths
.
get
(
pathname
+
filename
);
boolean
make
=
createFilePath
(
new
File
(
pathname
));
if
(
make
)
{
try
{
Files
.
write
(
path
,
list
);
}
catch
(
IOException
e
)
{
log
.
info
(
"生成文件时出现异常:"
+
e
.
getMessage
());
}
}
return
new
File
(
pathname
+
filename
);
}
public
File
createFileStms
(
String
subName
,
String
catalog
,
List
<
String
>
list
){
String
pathname
=
"files/"
+
subName
+
"/"
+
catalog
+
"/"
;
String
filename
=
getFileNameByDateStms
(
subName
,
catalog
);
String
fileNameTmp
=
getFileNameByDateStmsTmp
(
subName
,
catalog
);
Path
path
=
Paths
.
get
(
pathname
+
filename
);
boolean
make
=
createFilePath
(
new
File
(
pathname
));
if
(
make
)
{
try
{
File
file
=
File
.
createTempFile
(
filename
,
".tmp"
,
new
File
(
pathname
));
Files
.
write
(
path
,
list
);
new
File
(
pathname
+
filename
).
renameTo
(
new
File
(
pathname
+
fileNameTmp
));
file
.
delete
();
}
catch
(
IOException
e
)
{
log
.
info
(
"生成文件时出现异常:"
+
e
.
getMessage
());
}
}
return
new
File
(
pathname
+
filename
);
}
public
File
createFileStms
(
String
subName
,
String
catalog
,
List
<
String
>
list
,
int
count
){
String
pathname
=
"files/"
+
subName
+
"/"
+
catalog
+
"/"
;
String
filename
=
getFileNameByDateStms
(
subName
,
catalog
,
count
);
String
fileNameTmp
=
getFileNameByDateStmsTmp
(
subName
,
catalog
,
count
);
Path
path
=
Paths
.
get
(
pathname
+
filename
);
boolean
make
=
createFilePath
(
new
File
(
pathname
));
if
(
make
)
{
try
{
File
file
=
File
.
createTempFile
(
filename
,
".tmp"
,
new
File
(
pathname
));
Files
.
write
(
path
,
list
);
new
File
(
pathname
+
filename
).
renameTo
(
new
File
(
pathname
+
fileNameTmp
));
file
.
delete
();
}
catch
(
IOException
e
)
{
log
.
info
(
"生成文件时出现异常:"
+
e
.
getMessage
());
}
}
return
new
File
(
pathname
+
filename
);
}
/**
* 根据日期生成文件名
* 以天为单位
* 文件名格式:平台名-种类-时间(yyyyMMdd).txt
*/
private
String
getFileNameByDate
(
String
subName
,
String
catalog
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyyMMdd"
);
return
subName
+
"-"
+
catalog
+
"-"
+
sdf
.
format
(
new
Date
())
+
".txt"
;
}
private
String
getFileNameByDateStms
(
String
subName
,
String
catalog
,
int
count
)
{
return
subName
+
"-"
+
catalog
+
count
+
".tmp"
;
}
private
String
getFileNameByDateStmsTmp
(
String
subName
,
String
catalog
,
int
count
)
{
return
subName
+
"-"
+
catalog
+
count
+
".txt"
;
}
private
String
getFileNameByDateStms
(
String
subName
,
String
catalog
)
{
return
subName
+
"-"
+
catalog
+
".tmp"
;
}
private
String
getFileNameByDateStmsTmp
(
String
subName
,
String
catalog
)
{
return
subName
+
"-"
+
catalog
+
".txt"
;
}
/**
* 生成指定的文件夹路径
* 若文件夹不存在则创建
*/
private
boolean
createFilePath
(
File
file
)
{
return
file
.
exists
()
||
file
.
mkdirs
();
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论