In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use Groovy to read the code of local files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The following packages have been imported by default and do not need to be explicitly imported again using import:
Java.io.*
Java.lang.*
Java.math.BigDecimal
Java.math.BigInteger
Java.net.*
Java.util.*
Groovy.lang.*
Groovy.util.*
Runtime method invocation decision of Groovy
At run time, Groovy determines which method is executed based on the parameter type. Java, on the contrary, the method being called is determined during compilation, depending on the parameter type.
In Groovy, the methods which will be invoked are chosen at runtime. This is called runtime dispatch or multi-methods. It means that the method will be chosen based on the types of the arguments at runtime. In Java, this is the opposite: methods are chosen at compile time, based on the declared types.
The print result of the following code is 1:
Int method (String arg) {return 1;} int method (Object arg) {return 2;} Object o = "Object"; int result = method (o); println result
In Groovy, pairs of curly braces are used to declare closures, so the syntax for defining arrays uses square parentheses instead:
Int [] array = [1,2,3]
It is the default parameter for closures in Groovy:
Closures may have 1...N arguments, which may be statically typed or untyped. The first parameter is available via an implicit untyped argument named it if no explicit arguments are named. If the caller does not specify any arguments, the first parameter (and, by extension, it) will be null.
That means that a Groovy Closure will always have at least one argument, called it (if not specified otherwise) and it will be null if not given as a parameter.
Look at the code that uses Groovy to read the contents of local files, which is shorter than Java:
The contents of my file:
Output:
This method also works:
Complete code:
New File ('c:\\ temp\\ 1.txt'). EachLine ('UTF-8') {println "new line- >" + it} new File (' c:\\ temp\\ 1.txt'). WithReader ('UTF-8') {reader-> reader.eachLine {println "Another line:" + it}} "how to use Groovy to read local file code" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.