`
IT求知
  • 浏览: 14169 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

groovy 检测文件夹,有新文件就拷贝 windows版

阅读更多
import groovy.xml.MarkupBuilder
import java.text.SimpleDateFormat


/**
* 根据xml文件中的timeType是决定interval是什么单位的时间间隔。
* 从当前时间算起,此时间间隔内的文件就拷贝。
* @author UC181129
*
*/
public class StartProgram {

static main(agrs) {
def dir = 'C:\\usr\\workspace\\TestCopyFiles\\Property.xml'
def conf = new XmlParser().parse(dir)
def unit = 0L // the unit for interval
def timeUnit = conf.attribute('timeUnit')
def time = conf.attribute('interval').toLong()

switch(timeUnit) {
case 'day': unit = 1000 * 60 * 60 * 24; break
case 'hour': unit = 1000 * 60 * 60; break
case 'min': unit = 1000 * 60; break
case 'second': unit = 1000; break
default: unit = 1000; break
}

time = unit * time
def runTask = {src, interval ->
new Timer().schedule(new CopyFileByTimeStamp(src), new Date(), interval);
}
runTask(dir, time)

}
}


/**
* 大于配置文件中时间戳的文件都会被拷贝,复制过的不用再次复制
* 配置文件中的时间间隔为程序启动的时间间隔
* @author UC181129
*
*/
class CopyFileByTimeStamp extends TimerTask{
String dir

/**
* 构造函数
* @param path
*/
public CopyFileByTimeStamp(String path) {
this.dir = path
println 'program constracted'
}


/**
* 重写TimerTask的run()
*/
public void run() {

println 'programme start'

def params = new Params()
params.confDir = dir
getPropertyFile(params)

println 'programme stop ---------'
}


/**
* 读取配置文件信息
* @param params Params类型,里面存放所有用到的参数
* @return
*/
def static getPropertyFile(Params params) {
def conf = new XmlParser().parse(params.confDir)
params.lastFileConfDir = conf.attribute(params.LABEL_LASTFILECONFDIR)
params.timeType = conf.attribute(params.LABEL_TIMETYPE)
params.parseTime = new SimpleDateFormat(params.timeType)
params.timestamp = params.parseTime.parse(conf.attribute(params.LABEL_TIMESTAMP))    // convert to Date

if (!(new File(params.lastFileConfDir).exists())) {
writeToXML(params)
}

def LFTConf = new XmlParser().parse(params?.lastFileConfDir)
params.timestamp = params.parseTime.parse(LFTConf."$params.LABEL_TIMESTAMP".text())

conf.group.each {
copyFiles(it."$params.LABEL_SRC".text(), it."$params.LABEL_FGT".text(), params)
}
writeToXML(params)
}


/**
* 将最后处理的文件时间写入xml,如果刚开始没有没有该文件,则创建一个并以property.xml中信息为准
* @param params Params类型参数
* @return
*/
def static writeToXML(Params params) {
def file = new File(params?.lastFileConfDir)

// 如果有lastTime值,则比较大小,timestamp中记录日期最大的时间
if (params?.lastTime != null) {
def time = new Date(params.lastTime)
params.timestamp = (params.timestamp >= time)?params.timestamp:time
}

file.withWriter {
writer ->
new MarkupBuilder(writer).conf{
timestamp(params?.parseTime?.format(params?.timestamp))
timeType(params?.timeType)
}
}
}


/**
* 复制文件
* @param srcDir 源文件地址
* @param fgtDir 目标文件地址
* @param params Params类型参数
* @return
*/
def static copyFiles(srcDir, fgtDir, params) {
// if srcFiles not exit, return
def srcFiles = new File(srcDir)

if (!srcFiles.exists()) {
return
}

srcFiles.eachFile {
file ->
if (file.isFile() && isNewFile(file.lastModified(), params)) {
// if fgtDir not exit, create one
(new File(fgtDir)).mkdirs()
new File(fgtDir + File.separator + file.name).withWriter {
writer ->
file.eachLine {
line ->
writer.writeLine(line)
}
}
params.lastTime = (file.lastModified()>params.lastTime)?file.lastModified():params.lastTime
println 'copy ' + file.getCanonicalPath()
} else if (file.isDirectory()) {
copyFiles(srcDir + File.separator + file.name, fgtDir + File.separator + file.name, params)
}
}
}

/**
* 判断是否是需要传递的文件,以timestamp为标准,在该日期之后的返回TRUE,其他为FALSE
* @param fileTime 当前要判断的日期
* @param params Params类型参数
* @return
*/
static boolean isNewFile(fileTime, params) {
Date date = params.timestamp
Date dateFile = new Date(fileTime)
def result

if (date.compareTo(dateFile) >= 0) {
result = false
} else if(date.compareTo(dateFile) < 0)  {
result = true
}

return result
}
}

/**
* 该类各属性为公共参数
* @author UC181129
*
*/
class Params {
final String LABEL_INTERVAL = 'interval' // xml中标签interval
final String LABEL_TIMESTAMP = 'timestamp' // xml中标签timestamp
final String LABEL_TIMETYPE = 'timeType' // xml中标签timeType
final String LABEL_LASTFILECONFDIR = 'lastFileConfDir' // xml中标签lastFileConfDir
final String LABEL_SRC = 'src' // xml中标签src
final String LABEL_FGT = 'fgt' // xml中标签fgt
def timestamp // 时间戳,进行文件是否拷贝的判断依据
def timeType // 时间戳类型,进行日期装换时的format
def lastTime // 记录复制过的文件的最大时间
def confDir // Property.xml 的绝对路径
def lastFileConfDir // LastFileTime.xml的绝对路径,该文件记录上一次拷贝文件后,记录的最大文件时间
def parseTime // 用来保持根据timeType生成的SimpleDateFormat对象,方便其他程序使用
def interval // 程序启动的时间间隔
}
分享到:
评论

相关推荐

    groovy创建xml文件

    groovy 创建xml,并生成xml文件的代码

    Groovy-2.4.13 windows版安装程序.rar

    Windows版本的Groovy 2.4.13.exe安装文件,默认完整安装,也可根据需要自定义安装组件。本版本经过测试能够正常安装使用,支持添加到IDEA中。和JAVA类似的动态语言Groovy,在虚拟机中运行,Groovy脚本在运行时会先...

    Groovy 2.3.6 windows

    Groovy 2.3.6的Windows版本安装文件(exe),经过亲测,该版本能够使用,支持添加到IDEA中。

    springboot jpa 自动生成实体类的 文件 Generate POJOs.groovy

    springboot jpa 自动生成实体类的 文件 可以拿走直接用 Generate POJOs.groovy

    groovy2.5.6安装文件

    Groovy是一种基于JVM(Java虚拟机)的敏捷开发语言,它结合了Python、Ruby和Smalltalk的许多强大的特性,Groovy 代码能够与 Java 代码很好地结合,也能用于扩展现有代码。由于其运行在 JVM 上的特性,Groovy 可以...

    groovy-3.0.9-API文档-中文版.zip

    包含翻译后的API文档:groovy-3.0.9-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.codehaus.groovy:groovy:3.0.9; 标签:groovy、codehaus、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用...

    [Groovy] Making Java Groovy 英文版

    [Manning Publications] Making Java Groovy 英文版 [Manning Publications] Making Java Groovy E Book ☆ 图书概要:☆ Making Java Groovy is a practical handbook for developers who want to blend ...

    微服务项目中添加groovy文件技巧.docx

    软件项目的可扩展性和动态维护性是非常重要的,例如在微服务的网关动态过滤器中,要实现过滤器的动态更新而不影响项目的持续运行,就需要借助groovy的动态编译功能,而这个功能是通过 .groovy文件实现的。...

    apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本

    apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望大家多多下载,apache-groovy-3.0.8.zip apache官网的groovy3.0.8版本,希望...

    groovy-2.3.6-installer

    groovy-2.3.6-installer windows安装版本

    Groovy学习笔记 PDF版

    Groovy

    groovy+in+action_中文版

    groovy in action中文版,带标签

    groovy的eclipse插件

    groovy的eclipse插件,解压后拷贝至相应目录即可

    Gradle文件配置及groovy语法介绍.mp4

    Gradle文件配置及groovy语法介绍

    groovy-3.0.9-API文档-中英对照版.zip

    包含翻译后的API文档:groovy-3.0.9-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:org.codehaus.groovy:groovy:3.0.9; 标签:groovy、codehaus、jar包、java、中英对照文档; 使用方法:解压翻译后的...

    groovy-2.5.0-beta-2-installer.exe

    groovy-2.5.0 windows 安装包 Groovy 2.5 is the upcoming version of Groovy. For Windows installer 截至2017.11.09 groovy最新最稳定版本 md5: c8b6230728044db6399c837fcf6a23f2 大小:44.9 MB

    目前最新版groovy-2.4.11

    apache-groovy-binary-2.4.11.zip

    groovy-2.5.1-API文档-中文版.zip

    包含翻译后的API文档:groovy-2.5.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.codehaus.groovy:groovy:2.5.1; 标签:codehaus、groovy、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用...

    groovy-all-2.4.13-API文档-中文版.zip

    赠送Maven依赖信息文件:groovy-all-2.4.13.pom; 包含翻译后的API文档:groovy-all-2.4.13-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.codehaus.groovy:groovy-all:2.4.13; 标签:all、groovy、codehaus...

    groovy-loader-v2:在文件目录中动态加载Groovy脚本

    使用文件目录管理groovy bean:指定文件夹下的每一个一级文件夹对应一个namespace,某个一级文件夹下的所有groovy被注册成bean后会分配到同一个ApplicationContext 通过GroovyScriptFactory一级类加载器来实例化...

Global site tag (gtag.js) - Google Analytics