blaughli
blaughli

Reputation: 527

How to make custom file in Groovy

I have a text file that consists of a series of headers, each of which has a list of paths to files corresponding to that header

10gen
C:\cygwin\home\pro-services\git\mongodb\mongo\client\gridfs.cpp
C:\cygwin\home\pro-services\git\mongodb\mongo\client\model.cpp
10gen .SH
C:\cygwin\home\pro-services\git\mongodb\mongo\debian\mongod.1
C:\cygwin\home\pro-services\git\mongodb\mongo\debian\mongo.1

etc...

I am trying to create a new file for each of the headers, and the file will contain the related paths that are listed under the header in the original big file. I am a total newb to Groovy; how can I automate the creation these files?

Upvotes: 0

Views: 114

Answers (1)

ataylor
ataylor

Reputation: 66059

Something like this:

def output
new File("input.txt").eachLine { line ->
    if (isHeader(line)) {
        output?.close()
        output = new PrintWriter(new FileWriter(line))
    } else {
        output?.println(line)
    }
}
output?.close()

The isHeader method should return true if the line is a header.

Upvotes: 1

Related Questions