Browse Source

对删除txt文件进行优化

java8
wenkuan.shi 2 years ago
parent
commit
1a25c4d29b
  1. 54
      src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java

54
src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java

@ -107,6 +107,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
} }
} }
} }
} catch (IOException | DirectoryIteratorException e) { } catch (IOException | DirectoryIteratorException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -142,12 +143,11 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
//对图片保存 //对图片保存
if (CollectionUtils.isNotEmpty(imageFiles)) { if (CollectionUtils.isNotEmpty(imageFiles)) {
String timestamp = String.valueOf(System.currentTimeMillis()); String timestamp = String.valueOf(System.currentTimeMillis());
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (String imageFile : imageFiles) { for (String imageFile : imageFiles) {
int index = 1; int index = 1;
list.add(excel.getBackupPhotoPath() + "\\" + index + timestamp + "$" + imageFile);
list.add(excel.getBackupPhotoPath() + File.separator + index + timestamp + "$" + imageFile);
index++; index++;
} }
//将图片改成复制后的名字路径 //将图片改成复制后的名字路径
@ -156,7 +156,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
for (String s : imageFiles) { for (String s : imageFiles) {
int index = 1; int index = 1;
// 创建源文件对象和目标文件夹对象 // 创建源文件对象和目标文件夹对象
deleteBypath(excel.getFolderPath() + "\\" + s, excel.getBackupPhotoPath(), index + timestamp + "$" + s);
deleteBypath(excel.getFolderPath() + File.separator + s, excel.getBackupPhotoPath(), index + timestamp + "$" + s);
index++; index++;
} }
} }
@ -173,13 +173,13 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
String s = file.substring(dotIndex + 1); String s = file.substring(dotIndex + 1);
if (s.equals("csv") || s.equals("xls")) { if (s.equals("csv") || s.equals("xls")) {
fileName.add(excel.getFolderPath() + "\\" + file);
fileName.add(excel.getFolderPath() + File.separator + file);
} }
if (s.equals("tff")) { if (s.equals("tff")) {
tffName.add(excel.getFolderPath() + "\\" + file);
tffName.add(excel.getFolderPath() + File.separator + file);
} }
if (s.equals("txt")) { if (s.equals("txt")) {
txtName.add(excel.getFolderPath() + "\\" + file);
txtName.add(excel.getFolderPath() + File.separator + file);
} }
} }
//用一个统一的时间搓保证新增和复制删除的文件名是一样的 //用一个统一的时间搓保证新增和复制删除的文件名是一样的
@ -223,17 +223,18 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
for (String s : txtName) { for (String s : txtName) {
int index=1; int index=1;
//找到\\的最后索引 //找到\\的最后索引
int dotIndex = s.lastIndexOf("\\");
int dotIndex = s.lastIndexOf(File.separator);
String name = s.substring(dotIndex + 1); String name = s.substring(dotIndex + 1);
deleteBypath(s, excel.getBackupFolderPath(), index + timestamp1 + "$" + name); deleteBypath(s, excel.getBackupFolderPath(), index + timestamp1 + "$" + name);
index++; index++;
} }
} }
} }
//新增完之后把原路径的文件复制到另一个文件并删除原路径的下的文件 //新增完之后把原路径的文件复制到另一个文件并删除原路径的下的文件
//endPath本地文件名 suffixName复制到文件夹路径 //endPath本地文件名 suffixName复制到文件夹路径
private static void deleteBypath(String endPath, String suffixName, String newFileName) {
private static void deleteBypath(String endPath, String suffixName, String newFileName) throws Exception{
// 创建源文件对象和目标文件夹对象 // 创建源文件对象和目标文件夹对象
File sourceFile = new File(endPath); File sourceFile = new File(endPath);
@ -245,7 +246,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
if (!destinationDir.exists()) { if (!destinationDir.exists()) {
destinationDir.mkdirs(); // 创建目标文件夹及其父目录 destinationDir.mkdirs(); // 创建目标文件夹及其父目录
} }
// 构建目标文件路径
// 构建目标文件路径File.separator:"\\"
String destinationPath = suffixName + File.separator + newFileName; String destinationPath = suffixName + File.separator + newFileName;
File destinationFile = new File(destinationPath); File destinationFile = new File(destinationPath);
@ -253,16 +254,18 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
// 复制文件到目标文件夹 // 复制文件到目标文件夹
Files.copy(sourceFile.toPath(), destinationFile.toPath()); Files.copy(sourceFile.toPath(), destinationFile.toPath());
log.info("文件复制成功!" + sourceFile.toPath()); log.info("文件复制成功!" + sourceFile.toPath());
// 删除源文件 // 删除源文件
if (sourceFile.delete()){ if (sourceFile.delete()){
log.info("源文件删除成功!" + sourceFile.toPath());
log.info("文件删除成功 :" + sourceFile.toPath());
}else { }else {
log.info("源文件删除失败!" + sourceFile.toPath());
log.info("文件删除失败 :" + sourceFile.toPath());
} }
}catch (IOException e) { }catch (IOException e) {
log.info("文件操作出错:" + e.getMessage()); log.info("文件操作出错:" + e.getMessage());
} }
//存在就删除
} }
} }
@ -275,8 +278,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
int startLine = 15; // 第一次读取开始的行数 int startLine = 15; // 第一次读取开始的行数
int firstReadLines = 41; int firstReadLines = 41;
int columnInterval = 11; int columnInterval = 11;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(s));
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(s))){
String line; String line;
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
// 第一次读取从第 15 行开始的 41 行数据 // 第一次读取从第 15 行开始的 41 行数据
@ -369,7 +371,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
index++; index++;
} }
equipmentDataDetails.get(equipmentDataDetails.size()-1).setFileValue1(file); equipmentDataDetails.get(equipmentDataDetails.size()-1).setFileValue1(file);
bufferedReader.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -446,9 +448,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
String byName = name.substring(dotIndex + 1); String byName = name.substring(dotIndex + 1);
String file =excel.getBackupFolderPath()+"\\"+num + timestamp1 + "$" + byName; String file =excel.getBackupFolderPath()+"\\"+num + timestamp1 + "$" + byName;
num++; num++;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(name));
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(name))){
String line; String line;
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
for (int i = 1; i <= 19; i++) { for (int i = 1; i <= 19; i++) {
@ -550,5 +550,23 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
} }
} }
public static void main(String[] args) {
String file="D:\\Data-acquisitio\\Equipment-data-acquisition\\Label01001\\20240628GEN205.txt";
File sourceFile = new File(file);
try{
boolean deleted = sourceFile.delete();
if (!deleted){
Thread.sleep(2000); // 等待2秒钟
deleted = sourceFile.delete(); // 再次尝试删除
if (!deleted){
log.info("源文件再次删除失败!" + sourceFile.toPath());
}
} else {
log.info("源文件删除成功!" + sourceFile.toPath());
}
}catch (Exception e){
e.printStackTrace();
}
}
} }
Loading…
Cancel
Save