本文主要实现了扫描指定文件路径下的文件,递归扫描其子目录下的所有文件信息,示例文件为:

  要求将后缀为.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表中:


本文出自 “Forever Love” 博客,转载请务必保留此出处http://www.cnblogs.com/dwf07223/p/3999225.html

Java扫描指定文件路径下的文件并且递归扫描其子目录下的所有文件的更多相关文章

  1. Python 文件复制&按目录树结构拷贝&批量删除目录及其子目录下的文件

    文件复制&按目录树结构拷贝&批量删除目录及其子目录下的文件 by:授客 QQ:1033553122 测试环境: Python版本:Python 3.3.2 Win7 64 代码实践 # ...

  2. [MAC] Mac OS X下快速复制文件路径的方法

    在windows上复制当前目录的路径有一个特别方便的方式,只需要用鼠标点击路径栏,它就会自动变成像”D:\Downloads\tmp”这样的路径,如果要复制文件路径,只需要将目录路径和文件名拼接起来即 ...

  3. Java开发桌面程序学习(六)——拖动文件获得文件路径

    拖动获得文件路径 在windows软件中,很多软件都提供了拖动文件的打开文件的功能,JavaFx中也是有这功能,是通过监听器来实现的 监听器 setOnDragDetected(new EventHa ...

  4. 使用Class.getResource和ClassLoader.getResource方法获取文件路径

    自从转投Java阵营后,一直发下Java程序的路径读取异常麻烦,因此查阅了比较多的版本内容,整合了一份自己的学习笔记.主要使用Class及通过ClassLoader来动态获取文件路径. 查阅链接如下: ...

  5. Servlet总结二(文件路径)

    Servlet总结二(文件路径) 前言 前面我们说过ServletContext表示的是web容器中的上下文,下面我们也是用到ServletContext中的方法读取文件 读取WebRoot文件下的文 ...

  6. 【转载】使用Class.getResource和ClassLoader.getResource方法获取文件路径

    自从转投Java阵营后,一直发下Java程序的路径读取异常麻烦,因此查阅了比较多的版本内容,整合了一份自己的学习笔记.主要使用Class及通过ClassLoader来动态获取文件路径. 查阅链接如下: ...

  7. Jmeter系列(33)- 跨平台运行 Jmeter,CSV 文件路径如何设置?

    如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html 抛出问题 上一篇文章中详细讲解了 CS ...

  8. 【转】[.Net] 确定当前网站的物理文件路径

    确定当前网站的物理文件路径 在应用程序中,您可能需要确定服务器上的文件或其他资源的路径.例如,如果应用程序以编程方式对文本文件进行读写操作,则必须为用于读取和写入的方法提供该文件的完整物理路径. 将物 ...

  9. Python中的相对文件路径的调用

    先让我们来看看一个用到相对文件路径的函数调用的问题.假设现在有两个脚本文件main.py和func.py,他们的路径关系是: . |--dir1 |--main.py |--dir2 |--func. ...

  10. python读取文件路径

    不同系统对文件路径的分割符不同: 在Windows系统下的分隔符是:\ (反斜杠). 在Linux系统下的分隔符是:/(斜杠). 绝对路径和相对路径 绝对路径就是文件的真正存在的路径,是指从硬盘的根目 ...

随机推荐

  1. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

  2. java线程与进程的比较

    线程具有许多传统进程所具有的特征,故又称为轻型进程(Light—Weight Process)或进程元:而把传统的进程称为重型进程(Heavy—Weight Process),它相当于只有一个线程的任 ...

  3. H3C RIPv2配置任务

  4. linux测试 scullpipe 驱动

    我们已经见到了 scullpipe 驱动如何实现阻塞 I/O. 如果你想试一试, 这个驱动的源码 可在剩下的本书例子中找到. 阻塞 I/O 的动作可通过打开 2 个窗口见到. 第一个可运行 一个命令诸 ...

  5. Python3使用过程中需要注意的点

    命名规则 变量 变量名只能是数字.字母或下划线的任意组合 变量名的第一个字符不能是数字 不能使用关键字作为变量名 变量的定义要具有可描述性 变量名不宜过长.不宜使用中文.拼音 常量(常用在配置文件中) ...

  6. How to use code to exit the application in UWP

    I will tell you how to exit the application in UWP by the code. We can call some static method to he ...

  7. The Zen of Python —— Python 之禅

    Beautiful is better than ugly.   # 优美好于丑陋(Python以编写优美的代码为目标) Explicit is better than implicit.   # 明 ...

  8. Delphi XE里的StrPas要注意哦(要让StrPas知道哪里是字符串结束)

    废话不多说了,直接上例子解说: procedure TForm1.Button1Click(Sender: TObject);var  aa: array[0..1]of AnsiChar;  bb1 ...

  9. jsp页面出错 Cannot call sendRedirect() after the response has been committed

    sendRedirect()不能多次调用,检查下代码

  10. Centos 7.5安装 Nginx 1.14.1

    1. 准备工作 查看系统版本 输入命令 cat /etc/redhat-release 我的Centos版本 CentOS Linux release 7.5.1804 (Core) 安装nginx所 ...