package com.changhong.util;
import com.changhong.pojo.DataLog;
import com.mysql.jdbc.JDBC4PreparedStatement;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class CommUtil {
public static void main(String args[]) {
// System.out.println(getLogTime(""));
String dirpath = "E:\\LOG";
CommUtil cu = new CommUtil();
cu.readAllFile(dirpath);
}
public void readAllFile(String path) {
File file= new File(path);
//判断传入的路径,是文件还是文件夹
if(!file.isDirectory()){
//是文件则进行数据读取
// System.out.println("wenjian1:"+path+"======"+file.getName());
CommUtil cu = new CommUtil();
cu.readFileContent(file);
}else if(file.isDirectory()) {
//是文件夹,则递归遍历
String[] filelist=file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(path);
readAllFile(path + "\\" + filelist[i]);//递归
}
}
}
//读取单个文件内容
public void readFileContent(File file){
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw
// File file = new File(pathName); // 要读取以上路径的文件
InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"GB2312"); // 建立一个输入流对象reader
BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
String line = br.readLine();
DataLog dataLog = new DataLog();
StringBuffer logContent = new StringBuffer("");
while (line != null) {
logContent.append(line);
line = br.readLine(); // 一次读入一行数据
}
dataLog.setId(getUUID());
dataLog.setIntime(getCurrentTime());//入库时间
dataLog.setLogtime(getLogTime(file.getName()));
dataLog.setDeviceid(getDeviceId(file.getName()));
dataLog.setLogcontent(logContent.toString());
insertDataLog(dataLog);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Connection conn = Connections.getConnection();
private static PreparedStatement ps = null;
private static String sql = "INSERT INTO datalog_tf(id,deviceid,logtime,intime,logcontent) VALUES(?,?,?,?,?)";
int i=1;
public void insertDataLog(DataLog dataLog){
// conn = Connections.getConnection();
try {
ps = conn.prepareStatement(sql);
ps.setString(1, dataLog.getId());
ps.setString(2, dataLog.getDeviceid());
ps.setString(3, dataLog.getLogtime());
ps.setString(4, dataLog.getIntime());
ps.setString(5, dataLog.getLogcontent());
String sql22 = ((JDBC4PreparedStatement)ps).asSql();
System.out.println(sql22);
ps.executeUpdate(); //执行sql语句
} catch (SQLException e) {
e.printStackTrace();
}finally {
// Connections.Close_ALL(conn,null,null);
}
}
//获取uuid
public static String getUUID(){
UUID uuid=UUID.randomUUID();
String str = uuid.toString();
String uuidStr=str.replace("-", "");
return uuidStr;
}
//获取系统当前时间
public static String getCurrentTime(){
Date day=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
return df.format(day);
}
//获取日志时间
public static String getLogTime(String fileName){
if(fileName.length()<35){
return "";
}
String logTime="";
if(fileName.contains(".txt")){
logTime =fileName.substring(18,35);
}else{
logTime =fileName.substring(13,28);
}
return logTime.replace("-","").replace("_","");
}
//获取deviceid
public static String getDeviceId(String fileName){
if(fileName.length()<35){
return "";
}
String deviceId="";
if(fileName.contains(".txt")){
deviceId =fileName.substring(5,17);
}else{
deviceId =fileName.substring(0,12);
}
return deviceId;
}
}