public static boolean checkLogFileInfos(String path, String fileName) {
boolean bool = false;
File file = new File(path + fileName);
// 如果打印信息的日志文件的大小大于1024KB
System.out.println(file.length() / 1024);
System.out.println(Long.parseLong(properties
.getProperty("LOG_INFOS_FILE_MAXLENGTH")));
if ((file.length() / 1024) >= Long.parseLong(properties
.getProperty("LOG_INFOS_FILE_MAXLENGTH"))) {
// 重命名本文件为当前日期+fileName
System.out.println(path + newName(fileName));
File newNamed = new File(path + newName(fileName));
file.renameTo(newNamed);
System.out.println(newNamed.getTotalSpace() / 1024);
// 重新建立一个新的日志文件
newFiles(path, fileName);
}
return bool;
}
private static String newName(String fileName) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");
String dateStr = format.format(date);
return dateStr + "_" + fileName;
} private static void newFiles(String path, String fileName) {
File file = new File(path + fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
checkLogFileInfos("C://", "abc.txt");
}
上面代码中不能重命名abc.txt文件,不能新建abc.txt文件,程序没有报错。
boolean bool = false;
File file = new File(path + fileName);
// 如果打印信息的日志文件的大小大于1024KB
System.out.println(file.length() / 1024);
System.out.println(Long.parseLong(properties
.getProperty("LOG_INFOS_FILE_MAXLENGTH")));
if ((file.length() / 1024) >= Long.parseLong(properties
.getProperty("LOG_INFOS_FILE_MAXLENGTH"))) {
// 重命名本文件为当前日期+fileName
System.out.println(path + newName(fileName));
File newNamed = new File(path + newName(fileName));
file.renameTo(newNamed);
System.out.println(newNamed.getTotalSpace() / 1024);
// 重新建立一个新的日志文件
newFiles(path, fileName);
}
return bool;
}
private static String newName(String fileName) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss");
String dateStr = format.format(date);
return dateStr + "_" + fileName;
} private static void newFiles(String path, String fileName) {
File file = new File(path + fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
checkLogFileInfos("C://", "abc.txt");
}
上面代码中不能重命名abc.txt文件,不能新建abc.txt文件,程序没有报错。
System.out.println(path + newName(fileName));这句话执行进去了吗?呵呵!
原因是file已经存在了,要么就file.delete(), file.createNewFile();rename同理看看返回值。要不就写个输入输出流,从新写一个新文件。
bool=file.renameTo(newNamed);
这个newNamed 换一个目录创建就可以了
import java.io.IOException;
import java.util.*;
import java.text.SimpleDateFormat;public class TestRename {
public static boolean checkLogFileInfos(String path, String fileName) {
boolean bool = false;
File file = new File(path + fileName);
// 如果打印信息的日志文件的大小大于1024KB
System.out.println(file.length() / 1024);
Properties properties = new Properties();
properties.setProperty("LOG_INFOS_FILE_MAXLENGTH","1");
System.out.println(Long.parseLong(properties.getProperty("LOG_INFOS_FILE_MAXLENGTH")));
if ((file.length() / 1024) >= Long.parseLong(properties.getProperty("LOG_INFOS_FILE_MAXLENGTH"))) {
// 重命名本文件为当前日期+fileName
/*******************这里的写法有点问题******文件名不能包含下列字符之一,你的日期格式中有冒号*******/
/* / \ < > : * ? " | */
File newNamed = new File(path + newName(fileName));
;file.renameTo(newNamed);
System.out.println(newNamed.getTotalSpace() / 1024);
//
// // 重新建立一个新的日志文件
System.out.println(fileName);
try {
new File(path + fileName).createNewFile();//这里写法也是有问题的 这里写法也是有问题的 这里写法也是有问题的
} catch (IOException e) {
e.printStackTrace();
}
}
return bool;
}
private static String newName(String fileName) {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hhmmss");// 把它改过来吧把它改过来吧把它改过来吧把它改过来吧把它改过来吧
String dateStr = format.format(date);
return dateStr + "_" + fileName;
} public static void main(String[] args) {
checkLogFileInfos("C:/", "abc.txt");
}
}