Java掃描指定文件路徑下的文件並且遞歸掃描其子目錄下的所有文件


 

  本文主要實現了掃描指定文件路徑下的文件,遞歸掃描其子目錄下的所有文件信息,示例文件為:

 

  要求將后綴為.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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM