import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MysqlLog {
public static final String folder_name = "D:\\mysql_log\\";
public static final String file_index_name = "D:\\mysql_log\\binary_log.index";
public static final String file_txt_name = "D:\\mysql_log\\binary_log.txt";
public static final String mysql_binlog_exe = "D:\\mysql_log\\mysqlbinlog.exe";
public static final String insert = "insert";
public static final String update = "update";
public static final String delete = "delete from";
public static void main(String[] args) throws Exception {
String lastFileName = readLastLine(file_index_name, "utf-8");
System.out.println(lastFileName);
convertToTxt(lastFileName);
readContent(new File(file_txt_name), "utf-8");
String actionSql = lastActionSql(file_txt_name, "utf-8");
System.out.println(actionSql);
}
public static boolean isExistActionSql(final String content,final String... actionSqls){
for(String actionSql : actionSqls){
Pattern pattern = Pattern.compile(actionSql);
Matcher matcher = pattern.matcher(content);
if(matcher.find()){
return true;
}
}
return false;
}
public static String lastActionSql(String fileName,String charset){
String actionSql = "";
RandomAccessFile rf = null;
try {
rf = new RandomAccessFile(fileName, "r");
long len = rf.length();
long start = rf.getFilePointer();
long nextend = start + len - 1;
String line;
rf.seek(nextend);
int c = -1;
while (nextend > start) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
if (line != null) {
if(isExistActionSql(line,insert,update,delete)){
actionSql = new String(line.getBytes("ISO-8859-1"),
charset);
return actionSql;
}
}
nextend--;
}
nextend--;
rf.seek(nextend);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return actionSql;
}
public static String readContent(File file, String charset) {
StringBuffer sb = new StringBuffer();
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
inputStreamReader = new InputStreamReader(
new FileInputStream(file), charset);
bufferedReader = new BufferedReader(inputStreamReader);
String content = "";
while ((content = bufferedReader.readLine()) != null) {
System.out.println(content);
}
System.out.println(sb.toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStreamReader.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void convertToTxt(String fileName) {
Runtime runtime = Runtime.getRuntime();
String cmd = "cmd.exe /c " + mysql_binlog_exe + " " + fileName + " > "
+ file_txt_name;
try {
runtime.exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String readLastLine(String filename, String charset) {
String lastFileName = "";
RandomAccessFile rf = null;
try {
rf = new RandomAccessFile(filename, "r");
long len = rf.length();
long start = rf.getFilePointer();
long nextend = start + len - 1;
String line;
rf.seek(nextend);
int c = -1;
while (nextend > start) {
c = rf.read();
if (c == '\n' || c == '\r') {
line = rf.readLine();
if (line != null) {
lastFileName = new String(line.getBytes("ISO-8859-1"),
charset);
return lastFileName;
}
nextend--;
}
nextend--;
rf.seek(nextend);
// if (nextend == 0) {// 当文件指针退至文件开始处,输出第一行
// lastFileName += new
// String(rf.readLine().getBytes("ISO-8859-1"), charset);
// }
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rf != null)
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return lastFileName;
}
public String getTime() {
Calendar now = Calendar.getInstance(); // get the current time!
String time, mon, dd, hh, mm, sec, mes;
// format the time!
int month = now.get(Calendar.MONTH) + 1; // 月份必须加1,比如是12月,get返回的是11
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
int mesl = now.get(Calendar.MILLISECOND);
/*
* 位数不足前面补0
*/
if (month < 10)
mon = "0" + month;
else
mon = "" + month;
if (day < 10)
dd = "0" + day;
else
dd = "" + day;
if (hour < 10)
hh = "0" + hour;
else
hh = "" + hour;
if (minute < 10)
mm = "0" + minute;
else
mm = "" + minute;
if (second < 10)
sec = "0" + second;
else
sec = "" + second;
if (mesl < 10)
mes = "00" + mesl;
else if (mesl < 100)
mes = "0" + mesl;
else
mes = "" + mesl;
time = now.get(Calendar.YEAR) + mon + dd + hh + mm + sec + mes;
return time;
}
}