This article covers some of these details in the Groovy programming language , which is based on Java but with a different set of priorities that make Groovy feel more like Python. The first thing a new-to-Groovy programmer sees is that it is much less verbose than Java. The next observation is that it is by default dynamically typed. The third is that Groovy has closures, which are somewhat like lambdas in Java but provide access to the entire enclosing context Java lambdas restrict what can be accessed.
I'll jump off from his Java code to show you how it's done in Groovy. Since Groovy is based on Java, it requires a Java installation. You may be able to find a recent and decent version of Java and Groovy in your Linux distribution's repositories.
Or you can install Groovy by following the instructions on Groovy's download page. A nice alternative for Linux users is SDKMan , which you can use to get multiple versions of Java, Groovy, and many other related tools. Groovy looks like Java but less verbose. The first thing to notice is that all those import statements are already done in the background.
And since Groovy is partly intended to be a scripting language, by omitting the definition of the surrounding class and public static void main , Groovy will construct those things in the background. The semicolons are also gone. Groovy supports their use but doesn't require them except in cases like when you want to put multiple statements on the same line. Aaaaaaaaand the single quotes—Groovy supports either single or double quotes for delineating strings, which is handy when you need to put double quotes inside a string, like this:.
If you want to get the size of the file one can use the length property of the file class to get the size of the file. If you want to see if a path is a file or a directory, one can use the isFile and isDirectory option of the File class. If you want to create a new directory you can use the mkdir function of the File class. If you want to delete a file you can use the delete function of the File class. Groovy also provides the functionality to copy the contents from one file to another.
The file Example1. Here is an example code that reads the contents of a Groovy file as a UTF-8 string:. Sometimes we wish to read the contents line by line. Here is a sample code that reads the contents of a Groovy file line by line. Each line is shown on the screen with the corresponding line number. If we want full control, we can read all the lines of a file as a list. Here is an example code that reads the contents of a Groovy file and assign the contents to a list.
There are multiple ways on how we can write to a file. Below is an example code that writes contents to a file using the write method and the left shift operator. If we built the contents of what we wish to write to a String, we can write the contents in one go! We can assign the value to the text property of a file and it will automatically be written to the file. Deleting a file in Groovy is similar on how it is done in Java.
Here is a simple example that deletes a single file:. Here is an example code that search for all files given a file name pattern, and then delete them one by one. Renaming a file in Groovy is similar on how it is done in Java. Here is a simple example that renames a single file:. Here are some scenarios on how we can list files and directories in Groovy. Groovy file has convenience methods to help simplify code:.
Here is an example code on how to list all files and subdirectories of a parent directory in Groovy. If we want to list only subdirectories of a parent directory, the eachDir function can be used.
Here is an example code on how to list only subdirectories of a parent directory in Groovy. String, int, groovy. Closure public Object eachLine String charset, int firstLine, Closure closure Iterates through this file line by line.
Parameters: charset - opens the file with a specified charset firstLine - the line number value used for the first line default is 1, set to 0 to start counting from 0 closure - a closure arg 1 is line, optional arg 2 is line number Returns: the last value returned by the closure Since: 1.
Closure public void eachObject Closure closure Iterates through the given file object by object. ObjectInputStream, groovy. Closure public Writable filterLine Closure closure Filters the lines of a File and creates a Writable in return to stream the filtered lines. Parameters: closure - a closure which returns a boolean indicating to filter the line or not Returns: a Writable closure Since: 1. Reader, groovy. Closure public void filterLine Writer writer, Closure closure Filter the lines from this File, and write them to the given writer based on the given closure predicate.
Parameters: writer - a writer destination to write filtered lines to closure - a closure which takes each line as a parameter and returns true if the line should be written to this writer. Reader, java. Writer, groovy. Closure public void filterLine Writer writer, String charset, Closure closure Filter the lines from this File, and write them to the given writer based on the given closure predicate.
Parameters: writer - a writer destination to write filtered lines to charset - opens the file with a specified charset closure - a closure which takes each line as a parameter and returns true if the line should be written to this writer. Closure public Writable filterLine String charset, Closure closure Filters the lines of a File and creates a Writable in return to stream the filtered lines. Parameters: charset - opens the file with a specified charset closure - a closure which returns a boolean indicating to filter the line or not Returns: a Writable closure Since: 1.
Closure public byte[] getBytes Read the content of the File and returns it as a byte[]. Returns: a String containing the content of the file Since: 1. Parameters: charset - the charset used to read the content of the file Returns: a String containing the content of the file Since: 1.
Parameters: bytes - the byte array to append to the end of the File Returns: the original file Since: 1. Parameters: text - the text to write to the File Returns: the original file Since: 1.
Returns: the created DataOutputStream Since: 1. Returns: a BufferedInputStream of the file Since: 1. Returns: an object input stream Since: 1. Parameters: classLoader - the class loader to use when loading the class Returns: an object input stream Since: 1. Returns: an object output stream Since: 1.
Returns: the created OutputStream Since: 1. Returns: the created PrintWriter Since: 1. Parameters: charset - the charset Returns: a PrintWriter Since: 1. Returns: a BufferedReader Since: 1. Returns: a BufferedWriter Since: 1. Parameters: append - true if data should be appended to the file Returns: a BufferedWriter Since: 1. Parameters: charset - the name of the encoding used to write in this file Returns: a BufferedWriter Since: 1. Parameters: charset - the name of the encoding used to write in this file append - true if in append mode Returns: a BufferedWriter Since: 1.
Parameters: charset - the name of the encoding used to write in this file append - true if in append mode writeBom - whether to write a BOM Returns: a BufferedWriter Since: 2. Returns: a byte array with the contents of the file. Returns: a List of lines Since: 1. Reader public List readLines String charset Reads the file into a list of Strings, with one item for each line. Parameters: charset - opens the file with a specified charset Returns: a List of lines Since: 1.
Reader public String relativePath File to Relative path to file. Parameters: to - the File to calculate the path to Returns: the relative path between the files public boolean renameTo String newPathName Renames the file. It's a shortcut for File renameTo File Parameters: newPathName - The new pathname for the named file Returns: true if and only if the renaming succeeded; false otherwise Since: 1.
Parameters: bytes - the byte[] to write to the file Since: 1. Parameters: text - the text to write to the File Since: 1. String public void setText String text, String charset Synonym for write text, charset allowing: myFile. String, java. String public long size Provide the standard Groovy size method for File. Returns: the file's size length Since: 1. Parameters: regex - the delimiting regular expression closure - a closure Returns: the last value returned by the closure Since: 1.
String, groovy. Closure public Object splitEachLine String regex, String charset, Closure closure Iterates through this file line by line, splitting each line using the given regex separator.
Parameters: regex - the delimiting regular expression charset - opens the file with a specified charset closure - a closure Returns: the last value returned by the closure Since: 1.
Closure public Object splitEachLine Pattern pattern, Closure closure Iterates through this file line by line, splitting each line using the given separator Pattern. Parameters: pattern - the regular expression Pattern for the delimiter closure - a closure Returns: the last value returned by the closure Since: 1.
Pattern, groovy. Closure public Object splitEachLine Pattern pattern, String charset, Closure closure Iterates through this file line by line, splitting each line using the given regex separator Pattern. Parameters: pattern - the regular expression Pattern for the delimiter charset - opens the file with a specified charset closure - a closure Returns: the last value returned by the closure Since: 1.
0コメント