Java code//线程调用的方法
public static boolean checkLogFileInfos(String path, String fileName) {
boolean bool = false;
// 判断fileName在Path中是否存在
isExistTxtFile(path, fileName);
File file = new File(path + "\\" + fileName);
// 如果打印信息的日志文件的大小大于1024KB
if ((file.length() / 1024) >=12)) {
// 首先建立临时文件夹
System.out.println("创建临时文件夹" + path + "\\" + "TEMPFILES");
File tempFile = new File(path + "\\" + "TEMPFILES");
tempFile.mkdir();
// 将文件转移到临时文件夹中
System.out.println("将文件" + file.getName() + "转移到"
+ tempFile.getName() + "中");
copyFiletoPath(path, path + "\\"
+ properties.getProperty("LOG_TEMP_FILE_NAME"), fileName,
"");
// 将临时文件夹中的文件进行重命名,将临时文件夹中的文件转移到上一级目录中。
File tempFileTxt = new File(path + "\\"
+ properties.getProperty("LOG_TEMP_FILE_NAME") + "\\"
+ fileName);
String newNameStr = newName(fileName);
File newTempFileTxt = new File(path + "\\" + newNameStr);
System.out.println("开始重命名" + tempFileTxt.getPath());
bool = tempFileTxt.renameTo(newTempFileTxt);
if (bool) {
System.out.println(tempFileTxt.getName() + "重命名成功" + bool);
} else {
System.out.println(tempFileTxt.getName() + "重命名失败" + bool);
}
// 建立新的文件
boolean bool1 = file.delete();
System.out.println("删除" + file.getPath() + "的结果是" + bool1);
System.out.println("开始创建新的文件" + file.getPath());
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("创建" + file.getName() + "失败" + bool);
e.printStackTrace();
}
// 删除临时文件夹
System.out.println("删除" + tempFile.getPath());
boolean bool2 = tempFile.delete();
System.out.println("删除" + tempFile.getPath() + "结果是" + bool2);
boolean bool3 = tempFileTxt.delete();
System.out.println("删除" + tempFileTxt.getPath() + "结果是" + bool3);
}
return bool;
}
//相关的方法:
private static String newName(String fileName) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
String dateStr = format.format(date);
return dateStr + "" + fileName;
}
private static void isExistTxtFile(String path, String fileName) {
boolean bool = false;
File file = new File(path);
File[] files = file.listFiles();
if (files.length > 0) {
for (int i = 0; i < files.length; i++) {
if (files[i].getName().equals(fileName)) {
bool = true;
}
}
if (!bool) {
File newFile = new File(path + "\\" + fileName);
try {
boolean isOkNewFile = newFile.createNewFile();
if (isOkNewFile) {
System.out.println("文件创建成功");
} else {
System.out.println("文件创建失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
bool = false;
File newFile = new File(path + "\\" + fileName);
try {
boolean isOkNewFile = newFile.createNewFile();
if (isOkNewFile) {
System.out.println("文件创建成功");
} else {
System.out.println("文件创建失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String returnStardardTime() {
String stardardTime = "";
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(properties
.getProperty("TIME_TYPE"));
stardardTime = simpleDateFormat.format(date);
return stardardTime;
}
public static void copyFiletoPath(String fromPath, String toPath,
String fileName, String newFileName) {
int bytesum = 0;
int byteread = 0;
if (fileName.equals("") || fileName != null) {
File oldFile = new File(fromPath + "/" + fileName);
File newFile = null;
if (newFileName.equals("") || newFileName == null) {
newFile = new File(toPath + "/" + fileName);
} else {
newFile = new File(toPath + "/" + newFileName);
}
try {
if (oldFile.exists()) {
FileInputStream fileInputStream = new FileInputStream(
oldFile);
FileOutputStream fileOutputStream = new FileOutputStream(
newFile);
byte[] buffer = new byte[1024];
while ((byteread = fileInputStream.read(buffer)) != -1) {
bytesum += byteread;
fileOutputStream.write(buffer, 0, byteread);
}
fileInputStream.close();
fileOutputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void isExistTxtFile(String path, String fileName) {
boolean bool = false;
File file = new File(path);
File[] files = file.listFiles();
if (files.length > 0) {
for (int i = 0; i < files.length; i++) {
if (files[i].getName().equals(fileName)) {
bool = true;
}
}
if (!bool) {
File newFile = new File(path + "\\" + fileName);
try {
boolean isOkNewFile = newFile.createNewFile();
if (isOkNewFile) {
System.out.println("文件创建成功");
} else {
System.out.println("文件创建失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
bool = false;
File newFile = new File(path + "\\" + fileName);
try {
boolean isOkNewFile = newFile.createNewFile();
if (isOkNewFile) {
System.out.println("文件创建成功");
} else {
System.out.println("文件创建失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}