加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

groovy 速学 - 22 - IO

发布时间:2020-12-14 16:47:55 所属栏目:大数据 来源:网络整理
导读:目录 IO 控制台的输入输出 标准输出 格式化输出 标准输入 File 概述 写入文件 读取文件 常用方法 摘要 输入,输出,遍历行 IO 控制台的输入,输出 标准输出 print println 格式化输出 printf 标准输入 print ( "enter your name:" )def name = new BufferedR

目录

    • IO
      • 控制台的输入输出
        • 标准输出
        • 格式化输出
        • 标准输入
      • File
        • 概述
        • 写入文件
        • 读取文件
        • 常用方法

摘要

输入,输出,遍历行

IO

控制台的输入,输出

标准输出

  • print
  • println

格式化输出

  • printf

标准输入

print("enter your name:")
def name = new BufferedReader(new InputStreamReader(System.in)).readLine()
println "name is " + name

File

概述

Groovy 对 Java 的 File 类进行了增强,提供了抽象的,独立于系统的分级路径名,无论系统都应使用 / 作为分隔

写入文件

def file = new File("file.txt")
if (!file.exists()) {
  file.createNewFile()
} else {
  file.delete()
}
file.append("testn")

读取文件

def fis = new FileInputStream(file)
fis.eachLine { line ->
  println(line)
}

常用方法

  • delete(): Boolean
  • eachFile(clos) 遍历
  • eachFileResurse(clos) 遍历包括子目录
  • eachLine(clos)
  • getPath(): String 返回从抽象路径获得的真实路径
  • getText(): String 读取字符串
  • withPrintWriter(clos)
  • length(): long

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读