本篇內(nèi)容介紹了“怎么用Groovy讀取本地文件的代碼”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)紅河,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
下面這些包默認(rèn)已經(jīng)被導(dǎo)入了,不需要使用import再次顯式導(dǎo)入:
java.io.*
java.lang.*
java.math.BigDecimal
java.math.BigInteger
java.net.*
java.util.*
groovy.lang.*
groovy.util.*
運(yùn)行時(shí),Groovy根據(jù)參數(shù)類型決定具體哪一個(gè)方法被執(zhí)行。而Java恰恰相反,被調(diào)用的方法根據(jù)參數(shù)類型,在編譯期間就已經(jīng)定下來了。
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.
下列代碼的打印結(jié)果是1:
int method(String arg) { return 1;}int method(Object arg) { return 2;}Object o = "Object";int result = method(o);println result
在Groovy里,成對(duì)的大括號(hào)是聲明閉包用的,因此定義數(shù)組的語法改用中括號(hào):
int[] array = [1, 2, 3]
Groovy里的閉包,it為默認(rèn)參數(shù):
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.
看個(gè)用Groovy讀取本地文件內(nèi)容的代碼,和Java比起來短小精悍:
我的文件內(nèi)容:
輸出:
這種方法也行:
完整代碼:
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 } }
“怎么用Groovy讀取本地文件的代碼”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
本文題目:怎么用Groovy讀取本地文件的代碼
鏈接地址:http://chinadenli.net/article8/joeiip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、電子商務(wù)、品牌網(wǎng)站建設(shè)、Google、關(guān)鍵詞優(yōu)化、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)