这是Groovy中的一种方法:
// Get a writer to your new file
new File( '/tmp/newfile.txt' ).withWriter { w ->
// For each input file path
['/tmp/1.txt', '/tmp/2.txt', '/tmp/3.txt'].each { f ->
// Get a reader for the input file
new File( f ).withReader { r ->
// And write data from the input into the output
w << r << '\n'
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种方式(通过调用getText每个源文件)的优点是,在将内容写入之前,不需要将整个文件加载到内存中newfile.如果您的某个文件非常庞大,另一种方法可能会失败.