import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// checkLogs();
checkTheLogs();
// myTest();
}
public static void myTest() {
// List<File> fs = findTheFiles("app.log");
// for (File f : fs) {
// System.out.println(f.getName() + " time:" + f.lastModified());
// }
System.out.println(getLastLine(new File(
"E:\\mytest\\unrar\\newss\\smsmms.log")));
}
/**
* 持续检测日志
*/
private static void checkTheLogs() {
while (true) {
if (Test.isExecuted()) {
try {
// Thread.sleep(43200000);//十二小时
Thread.sleep(60000);
// Test.setExecuted(false);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (Test.isStoped()) {
break;
}
Test.checkLogs();
// BackupLogs.setStop_flag(true);
}
}
private static String backupPath = "E:\\mytest\\unrar\\old1";
private static String logSourcePath = "E:\\mytest\\unrar\\newss";
private static ExecutorService executorService = Executors
.newFixedThreadPool(9);
private static boolean executed = false;
private static boolean stop_flag = false;
public static boolean isExecuted() {
return executed;
}
public static void setExecuted(boolean executed) {
Test.executed = executed;
}
public static boolean isStoped() {
return stop_flag;
}
public static void setStop_flag(boolean stop_flag) {
Test.stop_flag = stop_flag;
}
/**
* 检测 备份日志
*/
public static void checkLogs() {
List<String> logKeys = new ArrayList<String>();
logKeys.add("app.log");
logKeys.add("radio.log");
logKeys.add("video.log");
logKeys.add("smsmms.log");
logKeys.add("video_service.log.log");
logKeys.add("smsmms.log");
logKeys.add("172.22.20.10.5060_messages.log");
logKeys.add("127.0.0.1.5060_events.log");
logKeys.add("172.22.20.10.5060_events.log");
for (final String logKey : logKeys) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
checkFiles(logKey);
}
});
// executorService.execute(t);
executorService.submit(t);
}
Test.setExecuted(true);
}
public static void shutDownThreadPool() {
executorService.shutdown();
}
public static void stopCheckLogFiles() {
setStop_flag(true);
executorService.shutdown();
}
/**
* 检测 备份某一类日志
*
* @param logKeyWord
* 日志类关键字
*/
private static void checkFiles(String logKeyWord) {
File oldFile = findLastestFile(logKeyWord);
// File[] newFiles = new File(logPath).listFiles();
List<File> fls = findTheFiles(logKeyWord);
if (fls == null) {
return;
}
File tempFile = null;
boolean addFlag = false;
String ol = null;
if (oldFile != null) {
ol = getLastLine(oldFile);
if (ol == null) {
addFlag = true;
}
} else {
addFlag = true;
}
int fnm = fls.size();
for (int i = (fnm - 1); i >= 0; i--) {
// for (int i = (newFiles.length - 1); i >= 0; i--) {
// tempFile = newFiles[i];
tempFile = fls.get(i);
if (addFlag) {
// 直接拷贝文件,因为这些文件都是没有的
try {
TransferFile.copyFile(tempFile, backupPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
BufferedWriter bw = null;
try {
BufferedReader obr = new BufferedReader(new FileReader(
tempFile));
String temp = obr.readLine();
while (temp != null) {
if (addFlag) {
// 将本文件的后续内容添加到一个新文件中去
if (bw == null) {
Date upDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMddHHmmssSSS");
bw = new BufferedWriter(new FileWriter(
new File(backupPath + "/"
+ sdf.format(upDate) + "_add_")
+ tempFile.getName()));
}
if (temp.trim() != null) {
bw.write(temp);
}
bw.newLine();
} else {
temp = temp.trim();
if ((temp != null) && (!"".equals(temp))
&& temp.equals(ol)) {
addFlag = true;
}
}
temp = obr.readLine();
}
obr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 如果源文件中都没有在备份文件夹中,则全部复制
if (!addFlag) {
for (int i = (fnm - 1); i >= 0; i--) {
try {
TransferFile.copyFile(fls.get(i), backupPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 在备份文件中找到某类最新的那个文件
*
* @param fileKeyWord
* 区分日志文件类的关键字
*/
private static File findLastestFile(String fileKeyWord) {
File[] oldFiles = new File(backupPath).listFiles();
File lf = null;
if (oldFiles != null) {
long tmp = 0;
for (File fl : oldFiles) {
String fn = fl.getName();
if (fn.contains(fileKeyWord)) {
long mdt = fl.lastModified();
if (tmp < mdt) {
tmp = mdt;
lf = fl;
}
}
}
}
return lf;
}
/**
* 找出某一类日志文件
*
* @param fileKeyWord
* 区分日志类的关键字
* @return
*/
private static List<File> findTheFiles(String fileKeyWord) {
List<File> fls = new ArrayList<File>();
File[] oldFiles = new File(logSourcePath).listFiles();
for (File fl : oldFiles) {
String fn = fl.getName();
if (fn.contains(fileKeyWord)) {
fls.add(fl);
}
}
// 对文件进行先后顺序的排序
int sz = fls.size();
if (sz == 11) {
File fa = fls.get(2);
fls.remove(2);
fls.add(fa);
}
// for (int i = 0; i < (sz - 1); i++) {
// for (int j = (i + 1); j < sz; j++) {
// File fa = fls.get(i);
// File fb = fls.get(j);
// if (fa.lastModified() < fb.lastModified()) {
// File tmpf = fa;
// fls.set(i, fb);
// fls.set(j, tmpf);
// }
// }
// }
return fls;
}
/**
* 找出最后更新的行内容
*
* @param file
* 目标文件
* @return 最后更新的行内容
*/
private static String getLastLine(File file) {
String lastLine = null;
try {
BufferedReader obr = new BufferedReader(new FileReader(file));
try {
String temp = obr.readLine();
while (temp != null) {
if (temp.trim() != null) {
lastLine = temp.trim();
}
temp = obr.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lastLine;
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// checkLogs();
checkTheLogs();
// myTest();
}
public static void myTest() {
// List<File> fs = findTheFiles("app.log");
// for (File f : fs) {
// System.out.println(f.getName() + " time:" + f.lastModified());
// }
System.out.println(getLastLine(new File(
"E:\\mytest\\unrar\\newss\\smsmms.log")));
}
/**
* 持续检测日志
*/
private static void checkTheLogs() {
while (true) {
if (Test.isExecuted()) {
try {
// Thread.sleep(43200000);//十二小时
Thread.sleep(60000);
// Test.setExecuted(false);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (Test.isStoped()) {
break;
}
Test.checkLogs();
// BackupLogs.setStop_flag(true);
}
}
private static String backupPath = "E:\\mytest\\unrar\\old1";
private static String logSourcePath = "E:\\mytest\\unrar\\newss";
private static ExecutorService executorService = Executors
.newFixedThreadPool(9);
private static boolean executed = false;
private static boolean stop_flag = false;
public static boolean isExecuted() {
return executed;
}
public static void setExecuted(boolean executed) {
Test.executed = executed;
}
public static boolean isStoped() {
return stop_flag;
}
public static void setStop_flag(boolean stop_flag) {
Test.stop_flag = stop_flag;
}
/**
* 检测 备份日志
*/
public static void checkLogs() {
List<String> logKeys = new ArrayList<String>();
logKeys.add("app.log");
logKeys.add("radio.log");
logKeys.add("video.log");
logKeys.add("smsmms.log");
logKeys.add("video_service.log.log");
logKeys.add("smsmms.log");
logKeys.add("172.22.20.10.5060_messages.log");
logKeys.add("127.0.0.1.5060_events.log");
logKeys.add("172.22.20.10.5060_events.log");
for (final String logKey : logKeys) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
checkFiles(logKey);
}
});
// executorService.execute(t);
executorService.submit(t);
}
Test.setExecuted(true);
}
public static void shutDownThreadPool() {
executorService.shutdown();
}
public static void stopCheckLogFiles() {
setStop_flag(true);
executorService.shutdown();
}
/**
* 检测 备份某一类日志
*
* @param logKeyWord
* 日志类关键字
*/
private static void checkFiles(String logKeyWord) {
File oldFile = findLastestFile(logKeyWord);
// File[] newFiles = new File(logPath).listFiles();
List<File> fls = findTheFiles(logKeyWord);
if (fls == null) {
return;
}
File tempFile = null;
boolean addFlag = false;
String ol = null;
if (oldFile != null) {
ol = getLastLine(oldFile);
if (ol == null) {
addFlag = true;
}
} else {
addFlag = true;
}
int fnm = fls.size();
for (int i = (fnm - 1); i >= 0; i--) {
// for (int i = (newFiles.length - 1); i >= 0; i--) {
// tempFile = newFiles[i];
tempFile = fls.get(i);
if (addFlag) {
// 直接拷贝文件,因为这些文件都是没有的
try {
TransferFile.copyFile(tempFile, backupPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
BufferedWriter bw = null;
try {
BufferedReader obr = new BufferedReader(new FileReader(
tempFile));
String temp = obr.readLine();
while (temp != null) {
if (addFlag) {
// 将本文件的后续内容添加到一个新文件中去
if (bw == null) {
Date upDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMddHHmmssSSS");
bw = new BufferedWriter(new FileWriter(
new File(backupPath + "/"
+ sdf.format(upDate) + "_add_")
+ tempFile.getName()));
}
if (temp.trim() != null) {
bw.write(temp);
}
bw.newLine();
} else {
temp = temp.trim();
if ((temp != null) && (!"".equals(temp))
&& temp.equals(ol)) {
addFlag = true;
}
}
temp = obr.readLine();
}
obr.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 如果源文件中都没有在备份文件夹中,则全部复制
if (!addFlag) {
for (int i = (fnm - 1); i >= 0; i--) {
try {
TransferFile.copyFile(fls.get(i), backupPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 在备份文件中找到某类最新的那个文件
*
* @param fileKeyWord
* 区分日志文件类的关键字
*/
private static File findLastestFile(String fileKeyWord) {
File[] oldFiles = new File(backupPath).listFiles();
File lf = null;
if (oldFiles != null) {
long tmp = 0;
for (File fl : oldFiles) {
String fn = fl.getName();
if (fn.contains(fileKeyWord)) {
long mdt = fl.lastModified();
if (tmp < mdt) {
tmp = mdt;
lf = fl;
}
}
}
}
return lf;
}
/**
* 找出某一类日志文件
*
* @param fileKeyWord
* 区分日志类的关键字
* @return
*/
private static List<File> findTheFiles(String fileKeyWord) {
List<File> fls = new ArrayList<File>();
File[] oldFiles = new File(logSourcePath).listFiles();
for (File fl : oldFiles) {
String fn = fl.getName();
if (fn.contains(fileKeyWord)) {
fls.add(fl);
}
}
// 对文件进行先后顺序的排序
int sz = fls.size();
if (sz == 11) {
File fa = fls.get(2);
fls.remove(2);
fls.add(fa);
}
// for (int i = 0; i < (sz - 1); i++) {
// for (int j = (i + 1); j < sz; j++) {
// File fa = fls.get(i);
// File fb = fls.get(j);
// if (fa.lastModified() < fb.lastModified()) {
// File tmpf = fa;
// fls.set(i, fb);
// fls.set(j, tmpf);
// }
// }
// }
return fls;
}
/**
* 找出最后更新的行内容
*
* @param file
* 目标文件
* @return 最后更新的行内容
*/
private static String getLastLine(File file) {
String lastLine = null;
try {
BufferedReader obr = new BufferedReader(new FileReader(file));
try {
String temp = obr.readLine();
while (temp != null) {
if (temp.trim() != null) {
lastLine = temp.trim();
}
temp = obr.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lastLine;
}
}