Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Groovy处理文件_Java_File_Groovy_Jar - Fatal编程技术网

Java 使用Groovy处理文件

Java 使用Groovy处理文件,java,file,groovy,jar,Java,File,Groovy,Jar,我想对jar文件执行一些操作,比如添加文件夹/文件,然后删除它们 我可以用Java来实现。但我希望使用Groovy实现它 但对方法或类一无所知。我是Groovy的新手,愿意学习和享受它的特性 有人能给我提供一个非常基本的groovy脚本来添加文件夹/文件吗?或者如果可能的话,简单地给我提供一些使用jar文件的链接 那会很有帮助的 以下是创建文件夹+文件的简单示例: def ant = new AntBuilder() ant.mkdir( dir: 'C:\Folder\' ) def file

我想对jar文件执行一些操作,比如添加文件夹/文件,然后删除它们

我可以用Java来实现。但我希望使用Groovy实现它

但对方法或类一无所知。我是Groovy的新手,愿意学习和享受它的特性

有人能给我提供一个非常基本的groovy脚本来添加文件夹/文件吗?或者如果可能的话,简单地给我提供一些使用jar文件的链接


那会很有帮助的

以下是创建文件夹+文件的简单示例:

def ant = new AntBuilder()
ant.mkdir( dir: 'C:\Folder\' )
def file = new File( 'C:\Folder\', 'TestFile'+ '.txt').newWriter()
file.writeLine( 'File content' )
file.close

下面是一个Groovy脚本,演示如何使用AntBuilder更新jar文件。您为它提供了两个参数,jar文件和要添加到jar中的文件

def (jar, include) = [new File(args[0]), new File(args[1])].each { f -> 
    if (!f.exists()) {
        println "$f does not exist"
        System.exit(0);
    }
    f
}

def ant = new AntBuilder()
if (include.isDirectory()) {
    println "Adding contents of $include to $jar"
    ant.jar(update:true, destfile: jar.absolutePath) {
        fileset(dir: include.absolutePath)
    }
} else {
    println "Adding file $include to $jar"
    ant.jar(update:true, destfile: jar.absolutePath, includesfile: include.absolutePath)
}
您还可以使用中的类来处理jar文件


AntBuilder本质上是Ant的包装器,它允许您使用Groovy语法组合任务。有关可用的任务,请参阅,有关Groovy语法,请参阅Groovy。

Groovy在JVM上运行,因此您也可以在Groovy中使用java类。@Adi之前仔细阅读了我的问题answering@Finciothnx 4 d linkgroovy不提供这种现成的功能,这会简化你的代码我想处理.jar文件你能告诉我更多关于AntBuilder的信息吗?