- 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
寫入檔案
如果要寫入檔案,則需要使用 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 中的所有檔案及其子目錄(如果存在)中的所有檔案。