本文主要实现了扫描指定文件路径下的文件,递归扫描其子目录下的所有文件信息,示例文件为:
要求将后缀为.dat的文件夹信息也写入到数据库中,然后将.chk文件解析,将文件中对应的内容读出来写入到数据库,对应类为ChkFileParseFactroy,本文文件发现代码为:
package com.src.service.impl; import java.io.File; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Date; import java.util.Enumeration; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.src.dao.ChkFileDao; import com.src.dao.FileDao; import com.src.dao.impl.ChkFileDaoImpl; import com.src.dao.impl.FileDaoImpl; import com.src.factory.ChkFileParseFactroy; import com.src.service.FoundFileService; import com.src.util.Config; import com.src.util.UUIDUtil; /** * @ClassName: FoundFileServiceImpl * @Description: 扫描文件 * @date Aug 24, 2014 1:26:18 AM */ public class FoundFileServiceImpl implements FoundFileService { private static Logger logger = LoggerFactory.getLogger(FoundFileServiceImpl.class); private static String interDir; /** * @Title: foundFile * @Description: 获取接口文件目录 * @最后修改时间:Aug 24, 2014 1:26:50 AM */ public void foundFile() { interDir = Config.getInstance().interDir; if (!isExist(interDir)) { logger.info("The file path " + interDir + " does not exists."); } else { logger.info("The current scanning directory: " + interDir); foundFile(interDir); } } /** * @Title: foundFile * @Description: 获取接口文件目录下的文件列表 * @最后修改时间:Aug 24, 2014 1:27:40 AM * @param rootStr */ public void foundFile(String rootStr) { String host = Config.getInstance().host; // 获取配置文件中的IP地址 File root = new File(rootStr); File[] file = root.listFiles(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); FileDao fileDao = new FileDaoImpl(); ChkFileDao chkFileDao = new ChkFileDaoImpl(); /** 接口文件变量 */ String id = null; // UUID String interNo = null; // 接口号 String opTime; // 数据日期 String interFileName; // 源接口文件名 String sourceDir = null; // 源接口文件路径 String fileSize; // 源接口文件大小 String updateTime; // 源接口文件最后修改时间 int status = 6; // 接口文件FTP状态 String loadStatus = ""; // 接口文件Load状态 /** CHK文件变量 */ String totalNum; // 接口记录总数 String chkFileSize; // 接口文件总大小 int chkFileNum = 0; // 接口文件个数 String ftpStatus = ""; // FTP状态 for (File f : file) { interFileName = f.getName(); // 获取文件名 logger.info("The current scan the file: " + interFileName); id = UUIDUtil.getUUID(); // 获取ID, Unique sourceDir = f.getAbsolutePath().toString().replace("\\", "/"); if (interFileName.endsWith(".dat") || interFileName.endsWith(".DAT")) { updateTime = sdf.format(new Date(f.lastModified())); // 获取接口文件最后修改时间 String interID = interFileName.substring(11, 15); // 获取6位接口号 List<Map<String, String>> list = fileDao.getInterNoByID(interID); for (Map<String, String> map : list) { interNo = map.get("FULLINTERCODE"); } if ("M".equalsIgnoreCase(interNo.substring(0, 1))) { opTime = interFileName.substring(0, 6); } else { opTime = interFileName.substring(0, 8); } if (-1 != fileDao.insert(id, interFileName,updateTime, interNo, opTime, sourceDir, host,loadStatus, status)) { logger.info(" The inter file " + interFileName + " successfully logged into the database."); } else { logger.info(" The inter file " + interFileName + " already exists in the database."); } } if (f.isFile()) { if (interFileName.endsWith(".avl") || interFileName.endsWith(".AVL")) { fileSize = Long.toString(f.length()); // 获取文件大小 updateTime = sdf.format(new Date(f.lastModified())); // 获取接口文件最后修改时间 interNo = interFileName.substring(0, 6); // 获取6位接口号 opTime = getOpTime(interFileName); // 获取操作时间 if (-1 != fileDao.insert(id, interFileName, updateTime, interNo, opTime, sourceDir, fileSize, host, loadStatus, status)) { logger.info(" The inter file " + interFileName + " successfully logged into the database."); } else { logger.info(" The inter file " + interFileName + " already exists in the database."); } } else if (interFileName.endsWith(".chk") || interFileName.endsWith(".CHK")) { ChkFileParseFactroy chkFileParse = new ChkFileParseFactroy(); Map<String, String> chkFileMap = new LinkedHashMap<String, String>(); chkFileMap = chkFileParse.fileParsing(f); opTime = getOpTime(interFileName); // 获取操作时间 updateTime = sdf.format(new Date()); // 获取更新时间:系统当前时间 interNo = interFileName.substring(0, 6); chkFileNum = Integer.valueOf(chkFileMap.get("fileNum")); totalNum = chkFileMap.get("totalNum"); chkFileSize = chkFileMap.get("fileSize"); /** * 根据interNo和opTime字段判断表中是否存在该记录 */ if (!chkFileDao.query(interNo, opTime)) { chkFileDao.insert(id, interNo, opTime, chkFileNum, totalNum, chkFileSize, sourceDir, updateTime, ftpStatus, loadStatus); logger.info(" The CHK file " + interFileName + " successfully logged into the database."); } else { logger.info(" The CHK file " + interFileName + " already exists in the database."); } } else { logger.info("The file " + interFileName + " is illegal, pass it."); } } else if (f.isDirectory()) { foundFile(f.toString()); } } } /** * @Title: isExist * @Description: 判断文件目录是否存在 * @最后修改时间:Aug 28, 2014 10:54:13 AM * @param filePath * @return boolean 返回类型 */ public static boolean isExist(String filePath) { boolean exists = true; File file = new File(filePath); if (!file.exists()) { exists = false; } return exists; } /** * * @Title: unifiedSeparator * @Description: 获取文件路径,统一格式全部都以分隔符结束 * @最后修改时间:Aug 31, 2014 12:59:54 AM * @param interDirectory * @return String 返回类型 */ public static String unifiedSeparator(String interDirectory) { String separator = "/"; if (interDirectory.endsWith(separator)) { return interDirectory; } else { return interDirectory + separator; } } /** * @Title: getOpTime * @Description: 判断是日接口还是月接口,并获取相应的日期; * 月接口:例M24289201408231155 非月接口:A/I/P,I0606920140820230045 * @最后修改时间:Aug 31, 2014 1:00:16 AM * @param interDirectory * @return String 返回类型 */ public static String getOpTime(String interDirectory) { String opTimeString = null; try { if ("M".equalsIgnoreCase(interDirectory.substring(0, 1))) { opTimeString = interDirectory.substring(6, 12); } else { opTimeString = interDirectory.substring(6, 14); } } catch (Exception e) { logger.info("Failure to obtain the operating time: " + e.getMessage()); } return opTimeString; } }
示例演示存储为:
.dat后缀的文件夹和.avl文件存到inter_file_log表中:
.chk文件存到inter_log表中: