//遍历文件内容
def file=new File('../../hello-groovy.iml')
file.eachLine {
line->println( line) /* <?xml version="1.0" encoding="UTF-8"?> <module type="JAVA_MODULE" version="4"> <component name="NewModuleRootManager" inherit-compiler-output="true"> <exclude-output /> <content url="file://$MODULE_DIR$"> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" name="groovy-2.4.15" level="application" /> </component> </module>*/
}
//获取文件的内容
def text=file.getText()
println(text)
/* <?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4"> <component name="NewModuleRootManager" inherit-compiler-output="true"> <exclude-output /> <content url="file://$MODULE_DIR$"> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" name="groovy-2.4.15" level="application" /> </component> </module>*/
//获取文件另一种方式,按行读取,返回一个list集合
def result=file.readLines()
println(result.toListString())
//[<?xml version="1.0" encoding="UTF-8"?>, <module type="JAVA_MODULE" version="4">,
// <component name="NewModuleRootManager" inherit-compiler-output="true">,
// <exclude-output />, <content url="file://$MODULE_DIR$">,
// <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />,
// </content>, <orderEntry type="inheritedJdk" />,
// <orderEntry type="sourceFolder" forTests="false" />,
// <orderEntry type="library" name="groovy-2.4.15" level="application" />,
// </component>, </module>]
//获取文件部分内容(前20个字符)
def reader=file.withReader {
reader->char [] buffer=new char[20] reader.read(buffer) buffer
}
println(reader)//<?xml version="1.0"
//写文件 def writer=file.withWriter{闭包}
//文件拷贝,groovy会自己关闭流
def results=copy('../../hello-groovy.iml','../../hello-groovy32.iml')
println(results)//true or false
def copy(String sourcePath,String destationPath){
try{ //创建目标文件
def desFile=new File(destationPath) if(!desFile.exists()){
desFile.createNewFile()
} //开始拷贝
new File(sourcePath).withReader {
reader->def lines=reader.readLines()
desFile.withWriter {
writer->
lines.each {
line->writer.append(line+'\r\n')
}
}
} return true}catch (Exception e){
}return false}
//对象的读写
ApplicationManager.init()
def person=PersonManager.createPerson('li',34)
def result3=saveObject(person,'../../person.txt')
def result4=(Person)readObject('../../person.txt')
println("${result4.name}:::${result4.age}")
//保存对象
def saveObject(Object obj,String path){
try{ //目标文件创建
def desFile=new File(path) if(!desFile.exists()){
desFile.createNewFile()
} //开始写object
desFile.withObjectOutputStream {
out->out.writeObject(obj)
} return true}catch (Exception e){
}return false}
//读取对象
def readObject(String path){
def obj=nulltry{
def desFile=new File(path) if(!desFile.exists()|| desFile==null)return null
desFile.withObjectInputStream {input ->
obj=input.readObject()
}
}catch (Exception e){
}return obj}
随时随地看视频