6, I / O
l Groovy provides many useful methods to process I / O, including standard Java Reader / Writer, InputStream / OutputStream, File, and URL classes
l When using a closure to allow the resource to ensure proper shutdown regardless of whether there is abnormality, such as the following example traversal, even if an abnormality occurs, the file can be closed correctly:
Import java.io.file
New file ("foo.txt"). Eachline {println it}
l Use Reader / Writer: Handling resources through closing
Import java.io.file
New file ("foo.txt"). withreader {reader |
While (true) {
Line = reader.readline ()
...
}
}
l Groovy provides a simple way to perform a command line process, an expression returns a java.lang.process instance, with an In / Out / ErR stream (Translator: Nothing test)
Process = "ls -l" .execute ()
Process.in.eachline {line | Println line}