Groovy User Guide (6)

xiaoxiao2021-03-06  42

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}

转载请注明原文地址:https://www.9cbs.com/read-112690.html

New Post(0)