本文主要实现了扫描指定文件路径下的文件,递归扫描其子目录下的所有文件信息,示例文件为:
要求将后缀为.dat的文件夹信息也写入到数据库中,然后将.chk文件解析,将文件中对应的内容读出来写入到数据库,对应类为ChkFileParseFactroy,本文文件发现代码为:
1 package com.src.service.impl; 2 3 import java.io.File; 4 import java.net.InetAddress; 5 import java.net.NetworkInterface; 6 import java.net.SocketException; 7 import java.net.UnknownHostException; 8 import java.text.SimpleDateFormat; 9 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.Date; 12 import java.util.Enumeration; 13 import java.util.LinkedHashMap; 14 import java.util.Map; 15 import java.util.Set; 16 17 import org.slf4j.Logger; 18 import org.slf4j.LoggerFactory; 19 20 import com.src.dao.ChkFileDao; 21 import com.src.dao.FileDao; 22 import com.src.dao.impl.ChkFileDaoImpl; 23 import com.src.dao.impl.FileDaoImpl; 24 import com.src.factory.ChkFileParseFactroy; 25 import com.src.service.FoundFileService; 26 import com.src.util.Config; 27 import com.src.util.UUIDUtil; 28 29 /** 30 * @ClassName: FoundFileServiceImpl 31 * @Description: 扫描文件 32 * @date Aug 24, 2014 1:26:18 AM 33 */ 34 public class FoundFileServiceImpl implements FoundFileService { 35 36 private static Logger logger = LoggerFactory.getLogger(FoundFileServiceImpl.class); 37 private static String interDir; 38 39 /** 40 * @Title: foundFile 41 * @Description: 获取接口文件目录 42 * @最后修改时间:Aug 24, 2014 1:26:50 AM 43 */ 44 public void foundFile() { 45 interDir = Config.getInstance().interDir; 46 if (!isExist(interDir)) { 47 logger.info("The file path " + interDir + " does not exists."); 48 } else { 49 logger.info("The current scanning directory: " + interDir); 50 foundFile(interDir); 51 } 52 } 53 54 /** 55 * @Title: foundFile 56 * @Description: 获取接口文件目录下的文件列表 57 * @最后修改时间:Aug 24, 2014 1:27:40 AM 58 * @param rootStr 59 */ 60 public void foundFile(String rootStr) { 61 62 String host = Config.getInstance().host; // 获取配置文件中的IP地址 63 64 File root = new File(rootStr); 65 File[] file = root.listFiles(); 66 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 67 68 FileDao fileDao = new FileDaoImpl(); 69 ChkFileDao chkFileDao = new ChkFileDaoImpl(); 70 71 /** 接口文件变量 */ 72 String id = null; // UUID 73 String interNo = null; // 接口号 74 String opTime; // 数据日期 75 String interFileName; // 源接口文件名 76 String sourceDir = null; // 源接口文件路径 77 String fileSize; // 源接口文件大小 78 String updateTime; // 源接口文件最后修改时间 79 int status = 6; // 接口文件FTP状态 80 String loadStatus = ""; // 接口文件Load状态 81 82 /** CHK文件变量 */ 83 String totalNum; // 接口记录总数 84 String chkFileSize; // 接口文件总大小 85 int chkFileNum = 0; // 接口文件个数 86 String ftpStatus = ""; // FTP状态 87 88 for (File f : file) { 89 interFileName = f.getName(); // 获取文件名 90 logger.info("The current scan the file: " + interFileName); 91 92 id = UUIDUtil.getUUID(); // 获取ID, Unique 93 sourceDir = f.getAbsolutePath().toString().replace("\\", "/"); 94 95 if (interFileName.endsWith(".dat") || interFileName.endsWith(".DAT")) { 96 updateTime = sdf.format(new Date(f.lastModified())); // 获取接口文件最后修改时间 97 String interID = interFileName.substring(11, 15); // 获取6位接口号 98 List<Map<String, String>> list = fileDao.getInterNoByID(interID); 99 100 for (Map<String, String> map : list) { 101 interNo = map.get("FULLINTERCODE"); 102 } 103 104 if ("M".equalsIgnoreCase(interNo.substring(0, 1))) { 105 opTime = interFileName.substring(0, 6); 106 } else { 107 opTime = interFileName.substring(0, 8); 108 } 109 110 if (-1 != fileDao.insert(id, interFileName,updateTime, interNo, opTime, sourceDir, host,loadStatus, status)) { 111 logger.info(" The inter file " + interFileName + " successfully logged into the database."); 112 } else { 113 logger.info(" The inter file " + interFileName + " already exists in the database."); 114 } 115 116 } 117 118 if (f.isFile()) { 119 120 if (interFileName.endsWith(".avl") || interFileName.endsWith(".AVL")) { 121 fileSize = Long.toString(f.length()); // 获取文件大小 122 updateTime = sdf.format(new Date(f.lastModified())); // 获取接口文件最后修改时间 123 interNo = interFileName.substring(0, 6); // 获取6位接口号 124 opTime = getOpTime(interFileName); // 获取操作时间 125 126 if (-1 != fileDao.insert(id, interFileName, updateTime, interNo, opTime, sourceDir, fileSize, host, loadStatus, status)) { 127 logger.info(" The inter file " + interFileName + " successfully logged into the database."); 128 } else { 129 logger.info(" The inter file " + interFileName + " already exists in the database."); 130 } 131 132 } else if (interFileName.endsWith(".chk") || interFileName.endsWith(".CHK")) { 133 ChkFileParseFactroy chkFileParse = new ChkFileParseFactroy(); 134 Map<String, String> chkFileMap = new LinkedHashMap<String, String>(); 135 chkFileMap = chkFileParse.fileParsing(f); 136 137 opTime = getOpTime(interFileName); // 获取操作时间 138 updateTime = sdf.format(new Date()); // 获取更新时间:系统当前时间 139 interNo = interFileName.substring(0, 6); 140 chkFileNum = Integer.valueOf(chkFileMap.get("fileNum")); 141 totalNum = chkFileMap.get("totalNum"); 142 chkFileSize = chkFileMap.get("fileSize"); 143 144 /** 145 * 根据interNo和opTime字段判断表中是否存在该记录 146 */ 147 if (!chkFileDao.query(interNo, opTime)) { 148 chkFileDao.insert(id, interNo, opTime, chkFileNum, totalNum, chkFileSize, sourceDir, updateTime, ftpStatus, loadStatus); 149 logger.info(" The CHK file " + interFileName + " successfully logged into the database."); 150 } else { 151 logger.info(" The CHK file " + interFileName + " already exists in the database."); 152 } 153 154 } else { 155 logger.info("The file " + interFileName + " is illegal, pass it."); 156 } 157 } else if (f.isDirectory()) { 158 foundFile(f.toString()); 159 } 160 161 } 162 } 163 164 /** 165 * @Title: isExist 166 * @Description: 判断文件目录是否存在 167 * @最后修改时间:Aug 28, 2014 10:54:13 AM 168 * @param filePath 169 * @return boolean 返回类型 170 */ 171 public static boolean isExist(String filePath) { 172 boolean exists = true; 173 File file = new File(filePath); 174 if (!file.exists()) { 175 exists = false; 176 } 177 return exists; 178 } 179 180 /** 181 * 182 * @Title: unifiedSeparator 183 * @Description: 获取文件路径,统一格式全部都以分隔符结束 184 * @最后修改时间:Aug 31, 2014 12:59:54 AM 185 * @param interDirectory 186 * @return String 返回类型 187 */ 188 public static String unifiedSeparator(String interDirectory) { 189 String separator = "/"; 190 191 if (interDirectory.endsWith(separator)) { 192 return interDirectory; 193 } else { 194 return interDirectory + separator; 195 } 196 } 197 198 /** 199 * @Title: getOpTime 200 * @Description: 判断是日接口还是月接口,并获取相应的日期; 201 * 月接口:例M24289201408231155 非月接口:A/I/P,I0606920140820230045 202 * @最后修改时间:Aug 31, 2014 1:00:16 AM 203 * @param interDirectory 204 * @return String 返回类型 205 */ 206 public static String getOpTime(String interDirectory) { 207 String opTimeString = null; 208 try { 209 if ("M".equalsIgnoreCase(interDirectory.substring(0, 1))) { 210 opTimeString = interDirectory.substring(6, 12); 211 } else { 212 opTimeString = interDirectory.substring(6, 14); 213 } 214 } catch (Exception e) { 215 logger.info("Failure to obtain the operating time: " 216 + e.getMessage()); 217 } 218 return opTimeString; 219 } 220 221 }
示例演示存储为:
.dat后缀的文件夹和.avl文件存到inter_file_log表中:
.chk文件存到inter_log表中:
本文出自 “Forever Love” 博客,转载请务必保留此出处http://www.cnblogs.com/dwf07223/p/3999225.html。
时间: 2024-10-06 14:30:18