Gradle學習之三Groovy高階語法
一、概述
二、json操作詳解
def list = [
new Person(name:'zhangsan',age: 18),
new Person(name:'lisi',age: 18),
new Person(name:'wangwu',age: 18),
new Person(name:'zhaoliu',age: 18),
]
//序列化
def json = JsonOutput.toJson(list)
println(json)
def prettyJson = JsonOutput.prettyPrint(json)
println(prettyJson)
//案例:進行網路請求,並進行資料的反序列化
def respone = getNetworkData('http://api.qingyunke.com/api.php?key=free&appid=0&msg=%E5%8C%97%E4%BA%AC%E5%A4%A9%E6%B0%94')
println respone
//根據Url請求網路
def getNetworkData(String url){
def connection = new URL(url).openConnection()
connection.setRequestMethod('GET')
connection.connect()
def res = connection.content.text
//反序列化
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(res)
}
對XML處理
def xmlData = '''
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="COOKING">
<title lang="en">James Harden</title>
<author>L J</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>L J</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
'''
//序列化
def xmlSlurper = new XmlSlurper()
def bookstore = xmlSlurper.parseText(xmlData)
//列印屬性
println bookstore.book[0].title.text()
println bookstore.book[0].title.@lang
//深度優先遍歷
def titles = bookstore.depthFirst().findAll { book->
book.author.text()== 'L J'
}
println titles
def list = []
bookstore.book.each {book-> if(book.author.text() == 'L J'){
list.add(book.title.text())
} }
println list
//廣度遍歷查詢COOKING型別圖書
def result = bookstore.children()
.findAll { book-> book.@category == 'COOKING'}
.collect( book-> book.title.text())
println result
接下來看一下自定義XML字串
//建立XML字串,使用
def stringWriter = new StringWriter()
def mb = new MarkupBuilder(stringWriter)
mb.bookstore(){
//顯式指明屬性
book(category:'COOKING'){
title(lang:'en','Everyday Italian')
//直接設定標籤對應的value
author('Giada De Laurentiis')
year(2005)
price(30.00)
}
book(category:'COOKING'){
title(lang:'en','James Harden')
author('L J')
year(2010)
price(30.00)
}
book(category:'CHILDREN'){
title(lang:'en','Harry Potter')
author('J K. Rowling')
year(2005)
price(29.99)
}
book(category:'WEB'){
title(lang:'en','Learning XML')
author('L J')
year(2003)
price(1.99)
}
}
println stringWriter
執行結果:
<bookstore>
<book category='COOKING'>
<title lang='en'>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category='COOKING'>
<title lang='en'>James Harden</title>
<author>L J</author>
<year>2010</year>
<price>30.00</price>
</book>
<book category='CHILDREN'>
<title lang='en'>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category='WEB'>
<title lang='en'>Learning XML</title>
<author>L J</author>
<year>2003</year>
<price>1.99</price>
</book>
</bookstore>
最後看一下,將Bean物件轉換為xml:
/**
*宣告Bean類
*/
class Bookstore {
def books = [
new Book(category:'COOKING',lang: 'en',titleName: 'Everyday Italian',author:'Giada De Laurentiis',year: 2005,price: 200),
new Book(category:'COOKING',lang: 'ch',titleName: 'James Harden',author:'L J',year: 2005,price: 200),
new Book(category:'CHILDREN',lang: 'en',titleName: 'Harry Potter',author:'JK rowling',year: 2005,price: 200),
new Book(category:'WEB',lang: 'en',titleName: 'Learning XML',author:'L J',year: 2005,price: 200),
]
}
class Book{
String category
String lang
String titleName
String author
Number year
Number price
}
//例項化Bean物件
def bookstore = new Bookstore()
def stringWriter = new StringWriter()
def mb = new MarkupBuilder(stringWriter)
mb.bookstore{
//使用迴圈+閉包的形式建立xml結點
bookstore.books.each {book->
Book(category:book.category){
title(lang:book.lang,book.titleName)
author(book.author)
year(book.year)
price("$book.price \$")
}
}
}
println stringWriter
三、檔案操作詳解
def file = new File('path')
//遍歷檔案的每一行
file.eachLine{ line ->
println line
}
//獲取檔案的內容
def text = file.getText()
//獲取檔案內容 每一行(返回一個陣列)
def result = file.readLines()
//讀取100個字元
def reader = file.withReader{ reader->
charp[] buff = new char[100]
reader.read(buff)
return buff
}
//檔案拷貝
def copy(String sourcePath,String destPath){
try{
//確保目標檔案存在
def destFile = new File(destPath)
if(!destFile.exists()){
destFile.createNewFile()
}
//拷貝操作
new File(sourcePath).withReader{ reader->
def lines = reader.readLines()
destFile.withWriter{ writer ->
lines.each{line ->
writer.append(line+"\r\n")
}
}
}
}catch(Exception e){
e.printStackTrace()
}
}
物件檔案讀寫
def saveObject(Object object,String path){
try{
//確保目標檔案存在
def destFile = new File(destPath)
if(!destFile.exists()){
destFile.createNewFile()
}
//寫物件
destFile.withObjectOutputStream{ out->
out.writeObject(object)
}
}catch(Exception e){
e.printStackTrace()
}
}
def readObject(String path){
def result = null
try{
//確保目標檔案存在
def file = new File(path)
if(file == null || !file.exists()){
return null
}
//寫物件
file.withObjectInputStream{ in->
result = in.readObject()
}
}catch(Exception e){
e.printStackTrace()
}
return result;
}
注意:如果IO流物件是new的,則需要顯式的呼叫close方法
def file = new File('path')
def out = file.newObjectOutputStream()
...
out.close()
相關文章
- Gradle入門系列(二)——groovy高階語法Gradle
- Gradle入門系列(一)——groovy基礎語法Gradle
- Gradle 之語言基礎 GroovyGradle
- 不得不學之「 Gradle」 ② GroovyGradle
- groovy-gradle-setting.gradle和sourceGradle
- Android Gradle Groovy自動化構建進階篇AndroidGradle
- Typescript 高階語法進階TypeScript
- Groovy基礎語法-list
- groovy-gradle-task(二)Gradle
- SQL 高階語法 MERGE INTOSQL
- Groovy基礎語法-字串篇字串
- Gradle系列(一) Groovy 基礎Gradle
- PHP高階語法總結PHP
- Java高階語法之反射Java反射
- R語言學習-高階資料管理R語言
- Java動態指令碼Groovy,高階啊!Java指令碼
- Linux C語言高階學習第四天(C高階-函式)LinuxC語言函式
- python高階語法:繼承性Python繼承
- Gradle指南之從Groovy遷移到KotlinGradleKotlin
- redis學習——高階功能Redis
- tensorflow語法學習
- MarkDown語法學習
- 第十一章、高階語法與用法
- Google高階搜尋技巧之高階語法查詢指令Go
- Gradle 教程學習Gradle
- Go語言學習教程:xorm表基本操作及高階操作GoORM
- Haskell學習-高階函式Haskell函式
- MySql 學習之路-高階2MySql
- Gradle核心思想(三)Groovy快速入門指南Gradle
- 高階前端基礎-JavaScript抽象語法樹AST前端JavaScript抽象語法樹AST
- Scala基本語法學習
- Flutter Dart語法學習FlutterDart
- Java SE 語法學習Java
- MySQL高階學習筆記(二)MySql筆記
- git高階命令學習記錄Git
- markdown學習(基礎語法)
- PHP基本語法學習 常量PHP
- PHP基本語法學習 [常量]PHP