public static void updateLog(String code, String record, String fileName) {
try {
String content = code + " " + record
+ System.getProperty("line.separator");
File file = new File(fileName);
// file.mkdir();
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Log File updated");
} catch (IOException e) {
e.printStackTrace();
}
List list = new ArrayList();
try {
BufferedReader bw = new BufferedReader(new FileReader(fileName));
String line = null;
try {
while ((line = bw.readLine()) != null) {
list.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int max = list.size();// TXT lines
if (max > 10) {
one_key();
}
}
public static void one_key() {
// delete the first line
String filePath = "C:\\LOG";
String fileName = "AddLog.txt";
del(filePath, fileName);
// delete file
httpHandler hfc = new httpHandler();
String path = "C:\\LOG\\AddLog.txt";
hfc.deleteFile(path);
// copy the file
// File files = new File("C:\\LOG\\test\\");
// File file[] = files.listFiles();// 获得目录中的文件及子目录信息
// int i = (int) (Math.random() * file.length);
// fun(file, i);
}
public static void del(String filePath, String fileName) {
RandomAccessFile readFile = null;
try {
readFile = new RandomAccessFile(filePath + "\\" + fileName, "r");
readFile.readLine();
readFile.readLine();
readFile.readLine();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
RandomAccessFile writeFile = null;
try {
writeFile = new RandomAccessFile(filePath + "\\test\\" + fileName,
"rw");
String tempLine = null;
while ((tempLine = readFile.readLine()) != null) {
writeFile.writeBytes(tempLine);
writeFile.writeBytes("\r\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
readFile.close();
writeFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
public static void fun(File file[], int i) {
if (file[i].exists()) {// 如果文件存在
String name = file[i].getName();// 获取文件名
if (file[i].isFile() && name.endsWith(".txt")) { // 如果是文件并且后缀名为.txt
File dest = new File("c:\\LOG\\" + file[i].getName());
file[i].renameTo(dest);
} else {
int j = (int) (Math.random() * file.length);
fun(file, j);
}
} else {
int j = (int) (Math.random() * file.length);
fun(file, j);
}
}
我看到你的代码中有如下一行:
httpHandler hfc = new httpHandler();
但是在你的代码中并没有定义 httpHandler 这个类,因此在你调用 hfc.deleteFile(path) 时会出现问题。
你可以尝试定义这个类或者删除这行代码,来修复这个问题。
另外,如果你的代码真的需要使用这个类,你可能需要检查你的 classpath 是否设置正确,以及是否已经导入了这个类所在的包。