
- Groovy 教程
- Groovy - 首页
- Groovy - 概述
- Groovy - 环境
- Groovy - 基本语法
- Groovy - 数据类型
- Groovy - 变量
- Groovy - 运算符
- Groovy - 循环
- Groovy - 决策制定
- Groovy - 方法
- Groovy - 文件 I/O
- Groovy - 可选
- Groovy - 数字
- Groovy - 字符串
- Groovy - 范围
- Groovy - 列表
- Groovy - 映射
- Groovy - 日期和时间
- Groovy - 正则表达式
- Groovy - 异常处理
- Groovy - 面向对象
- Groovy - 泛型
- Groovy - 特性
- Groovy - 闭包
- Groovy - 注解
- Groovy - XML
- Groovy - JMX
- Groovy - JSON
- Groovy - DSL
- Groovy - 数据库
- Groovy - 构建器
- Groovy - 命令行
- Groovy - 单元测试
- Groovy - 模板引擎
- Groovy - 元对象编程
- Groovy 有用资源
- Groovy - 快速指南
- Groovy - 有用资源
- Groovy - 讨论
Groovy - 文件 I/O
在处理 I/O 时,Groovy 提供了许多辅助方法。Groovy 提供了更简单的类,以提供以下文件功能。
- 读取文件
- 写入文件
- 遍历文件树
- 读取和写入数据对象到文件
除此之外,您还可以始终使用下面列出的普通 Java 类进行文件 I/O 操作。
- java.io.File
- java.io.InputStream
- java.io.OutputStream
- java.io.Reader
- java.io.Writer
读取文件
以下示例将输出 Groovy 中文本文件的所有行。eachLine 方法是 Groovy 中 File 类中的内置方法,用于确保读取文本文件的每一行。
import java.io.File class Example { static void main(String[] args) { new File("E:/Example.txt").eachLine { line -> println "line : $line"; } } }
File 类用于实例化一个新对象,该对象以文件名作为参数。然后它获取 eachLine 函数,将其放入名为 line 的变量中并相应地打印它。
如果文件包含以下行,则将打印它们。
line : Example1 line : Example2
将文件内容作为整个字符串读取
如果要将文件的全部内容作为字符串获取,可以使用文件类的 text 属性。以下示例演示了如何执行此操作。
class Example { static void main(String[] args) { File file = new File("E:/Example.txt") println file.text } }
如果文件包含以下行,则将打印它们。
line : Example1 line : Example2
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
写入文件
如果要写入文件,则需要使用 writer 类将文本输出到文件。以下示例演示了如何执行此操作。
import java.io.File class Example { static void main(String[] args) { new File('E:/','Example.txt').withWriter('utf-8') { writer -> writer.writeLine 'Hello World' } } }
如果打开文件 Example.txt,您将看到“Hello World”打印到文件中。
获取文件大小
如果要获取文件大小,可以使用文件类的 length 属性来获取文件大小。以下示例演示了如何执行此操作。
class Example { static void main(String[] args) { File file = new File("E:/Example.txt") println "The file ${file.absolutePath} has ${file.length()} bytes" } }
以上代码将显示文件大小(以字节为单位)。
测试文件是否为目录
如果要查看路径是文件还是目录,可以使用 File 类的isFile 和isDirectory 选项。以下示例演示了如何执行此操作。
class Example { static void main(String[] args) { def file = new File('E:/') println "File? ${file.isFile()}" println "Directory? ${file.isDirectory()}" } }
以上代码将显示以下输出:
File? false Directory? True
创建目录
如果要创建新目录,可以使用 File 类的mkdir 函数。以下示例演示了如何执行此操作。
class Example { static void main(String[] args) { def file = new File('E:/Directory') file.mkdir() } }
如果目录 E:\Directory 不存在,则将创建它。
删除文件
如果要删除文件,可以使用 File 类的 delete 函数。以下示例演示了如何执行此操作。
class Example { static void main(String[] args) { def file = new File('E:/Example.txt') file.delete() } }
如果文件存在,则将被删除。
复制文件
Groovy 还提供了将一个文件的内容复制到另一个文件的功能。以下示例演示了如何执行此操作。
class Example { static void main(String[] args) { def src = new File("E:/Example.txt") def dst = new File("E:/Example1.txt") dst << src.text } }
将创建文件 Example1.txt,并将文件 Example.txt 的所有内容复制到此文件。
获取目录内容
Groovy 还提供了列出驱动器和驱动器中文件的功用。
以下示例演示了如何使用 File 类的listRoots 函数显示计算机上的驱动器。
class Example { static void main(String[] args) { def rootFiles = new File("test").listRoots() rootFiles.each { file -> println file.absolutePath } } }
根据计算机上可用的驱动器,输出可能会有所不同。在标准计算机上,输出将类似于以下输出:
C:\ D:\
以下示例演示了如何使用 File 类的eachFile 函数列出特定目录中的文件。
class Example { static void main(String[] args) { new File("E:/Temp").eachFile() { file->println file.getAbsolutePath() } } }
输出将显示目录 E:\Temp 中的所有文件
如果要递归显示目录及其子目录中的所有文件,则可以使用 File 类的eachFileRecurse 函数。以下示例演示了如何执行此操作。
class Example { static void main(String[] args) { new File("E:/temp").eachFileRecurse() { file -> println file.getAbsolutePath() } } }
输出将显示目录 E:\Temp 中的所有文件及其子目录(如果存在)中的所有文件。