public classMessageHistory {
Map lines = new HashMap();
String outputPath;public static voidmain(String[] args) {
MessageHistory messageHistory= newMessageHistory();
messageHistory.multiReadMessage();
messageHistory.outputCardRecordsToFile();
}publicMessageHistory() {
outputPath= System.getProperty("user.dir")+File.separator+formatDate(new Date()) + "—打卡记录.txt";
File f= newFile(outputPath);if(f.exists()){
f.delete();
}
}/*** 消息文件以work开头
* 读取文件*/
public voidmultiReadMessage(){
File dir= new File(System.getProperty("user.dir"));if(dir.getParentFile().exists()){for(File file:dir.listFiles()){if(file.getName().trim().matches("^work.*\\.txt$")){
System.out.println("start read "+file.getAbsolutePath());
readMessage(file.getAbsolutePath());
}
}
}
}public voidlineToMap(String line) {if (line.matches("^[A-Za-z0-9]{6}.*")) {int index = line.indexOf("(");int lastIndex = line.lastIndexOf(")");if (index > 0 && lastIndex > 0) {
String workCode= line.substring(0, index);
String name= line.substring(index + 1, lastIndex);
String dateStr= line.substring(lastIndex + 1).trim();
Date date=parseDate(dateStr);
String key= formatDate(date) + "--" +workCode;
MessageSenderInfo recoderBean=lines.get(key);if (recoderBean == null) {
recoderBean= newMessageSenderInfo(workCode, name);
recoderBean.setAtWorkTime(date);
}else{if (recoderBean.getOffWorkTime() == null) {
recoderBean.setOffWorkTime(date);
}else{if(date.getTime()
recoderBean.setAtWorkTime(date);
}else if(date.getTime() >recoderBean.getOffWorkTime().getTime()){
recoderBean.setOffWorkTime(date);
}
}
}
lines.put(key, recoderBean);
}
}
}public List>sortMessage(){
List> list = new ArrayList>(lines.entrySet());
Collections.sort(list,newMessageComparator());returnlist;
}/*** 输出到文件*/
public voidoutputCardRecordsToFile() {
printHeader();
System.out.println("总记录:"+lines.size());
List> list =sortMessage();for (Map.Entrye : list) {
MessageSenderInfo messageSenderInfo=e.getValue();
String line= formatDate(messageSenderInfo.getAtWorkTime()) + " "+formatStringLen(messageSenderInfo.getName(),8) + " ";if(messageSenderInfo.getOffWorkTime() == null){if(getHour(messageSenderInfo.getAtWorkTime()) >= 12){
line+= formatStringLen("",21);
}
line+=formatLongDate(messageSenderInfo.getAtWorkTime());
}else
if(messageSenderInfo.getOffWorkTime() != null){
line+= formatLongDate(messageSenderInfo.getAtWorkTime()) + " ";
line+=formatLongDate(messageSenderInfo.getOffWorkTime());
}
writeLine(outputPath, line);
}
}/*** 打印头部*/
public voidprintHeader(){
String line= formatStringLen("打卡日期",12)+formatStringLen("姓名",10)+formatStringLen("上班打卡",21)+formatStringLen("下班打卡",21);
writeLine(outputPath, line);
}/*** 格式化字符串长度
*@params
*@paramlen
*@return
*/
public String formatStringLen(String s, intlen) {if (len >s.getBytes().length) {
StringBuilder sBuilder= newStringBuilder(s);for (int i = 0; i < len - s.getBytes().length; i++) {
sBuilder.append(" ");
}returnsBuilder.toString();
}returns;
}public voidwriteLine(String path, String s) {
File f= newFile(path);
OutputStream os= null;try{
os= new FileOutputStream(f, true);
Writer wirte= new OutputStreamWriter(os, "UTF-8");
BufferedWriter bufferedWriter= newBufferedWriter(wirte);
bufferedWriter.write(s);
bufferedWriter.newLine();
bufferedWriter.flush();
}catch(FileNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{if (os != null) {try{
os.close();
os= null;
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}public voidreadMessage(String path) {
InputStream ins= null;
BufferedReader buf= null;try{
ins= newFileInputStream(path);
buf= new BufferedReader(newInputStreamReader(ins, codeString(path)));
String line= null;while ((line = buf.readLine()) != null) {
lineToMap(line.trim());
}
}catch(FileNotFoundException e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}catch(UnsupportedEncodingException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}finally{if (ins != null) {try{
ins.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}/*** 排序比较
*@authorsfit0734
**/
class MessageComparator implements Comparator>{
@Overridepublic int compare(Map.Entry e1, Map.Entrye2) {
MessageSenderInfo m1=e1.getValue();
MessageSenderInfo m2=e2.getValue();
String dateStr=formatDate(m1.getAtWorkTime());
String dateStr1=formatDate(m2.getAtWorkTime());int result =dateStr.compareTo(dateStr1);if(result != 0){returnresult;
}else{returnm1.getName().compareTo(m2.getName());
}
}
};publicDate parseDate(String dataStr) {
DateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//String dateString = formatter.format(currentTime);
Date date = null;try{
date=formatter.parse(dataStr);
}catch(ParseException e) {
e.printStackTrace();
}returndate;
}publicString formatDate(Date date) {
DateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
String dateString=formatter.format(date);returndateString;
}publicString formatLongDate(Date date) {
DateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString=formatter.format(date);returndateString;
}/*** 获取小时
*@paramd
*@return
*/
public intgetHour(Date d){
Calendar cal=Calendar.getInstance();
cal.setTime(d);returncal.get(Calendar.HOUR_OF_DAY);
}/*** 判断文件的编码格式
*@paramfileName :file
*@return文件编码格式
*@throwsIOException
*@throwsException*/
public String codeString(String fileName) throwsIOException {
BufferedInputStream bin= newBufferedInputStream(newFileInputStream(fileName));int p = (bin.read() << 8) +bin.read();
String code= null;switch(p) {case 0xefbb:
code= "UTF-8";break;case 0xfffe:
code= "Unicode";break;case 0xfeff:
code= "UTF-16BE";break;default:
code= "GBK";
}returncode;
}classMessageSenderInfo {
String workCode;
String name;
Date atWorkTime;
Date offWorkTime;publicMessageSenderInfo() {super();//TODO Auto-generated constructor stub
}publicMessageSenderInfo(String workCode, String name) {super();this.workCode =workCode;this.name =name;
}publicString getWorkCode() {returnworkCode;
}public voidsetWorkCode(String workCode) {this.workCode =workCode;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicDate getAtWorkTime() {returnatWorkTime;
}public voidsetAtWorkTime(Date atWorkTime) {this.atWorkTime =atWorkTime;
}publicDate getOffWorkTime() {returnoffWorkTime;
}public voidsetOffWorkTime(Date offWorkTime) {this.offWorkTime =offWorkTime;
}
}
}