在 Scala 中向檔案中寫入文字

Suraj P 2023年1月30日
  1. 在 Scala 中使用 PrintWriter 將文字寫入檔案
  2. 在 Scala 中使用 FileWriter 將文字寫入檔案
  3. 在 Scala 中使用 Java NIO(New Input/Output)包將文字寫入檔案
在 Scala 中向檔案中寫入文字

本文將討論將文字寫入 Scala 檔案的不同方法。

我們使用 java.io 物件將文字寫入 Scala 中的檔案,因為它依賴於 Java 物件來執行各種功能。

在 Scala 中使用 PrintWriter 將文字寫入檔案

必須執行以下步驟才能在 Scala 中寫入檔案。

  • 使用 fileName 建立一個 PrintWriter 物件。
  • 使用 write() 方法寫入檔案。
  • 使用 close 函式在完成寫操作後關閉檔案。

例子:

import java.io._

object MyObject {
    def main(args: Array[String]) {
        val writer = new PrintWriter(new File("C:\\Users\\srjsu\\OneDrive\\Desktop\\temp.txt"))  //specify the file path
        writer.write("Iron Man is the best")
        writer.close()
    }
}

輸出:

Iron Man is the best

執行程式後,輸出將寫入桌面資料夾中的 temp.txt 檔案。

在 Scala 中使用 FileWriter 將文字寫入檔案

必須執行以下步驟才能使用 FileWriter 在 Scala 中寫入檔案。

  • 使用 fileName 建立一個 FileWriter 物件。
  • 使用 write() 方法寫入檔案。
  • 使用 close 函式在完成寫操作後關閉檔案。

例子:

import java.io._

object MyObject {
    def main(args: Array[String]) {
        val writeFile = new File("C:\\Users\\srjsu\\OneDrive\\Desktop\\test.txt")
        val text = "We are learning Scala programming language"
        val writer = new BufferedWriter(new FileWriter(writeFile))
        writer.write(text)
        writer.close()
    }
}

輸出:

We are learning Scala programming language

執行程式後,上面的輸出將寫入桌面資料夾中的 test.txt 檔案。

在 Scala 中使用 Java NIO(New Input/Output)包將文字寫入檔案

這是在 Scala 中將文字寫入檔案的最佳方法。使用 Java NIO 包,我們可以以非常簡潔的方式寫入檔案,即輸入更少的程式碼。

示例 1:

import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets

object MyObject {
    def main(args: Array[String]) {
        val inputText = "I remembered black skies\nThe lightning all around me\nI remembered each flash\nAs time began to blur\nLike a startling sign\nThat fate had finally found me\nAnd your voice was all I heard\nThat I get what I deserve\nSo give me reason\nTo prove me wrong\nTo wash this memory clean\nLet the floods cross\nThe distance in your eyes";
        Files.write(Paths.get("C:\\Users\\srjsu\\OneDrive\\Desktop\\test.txt"), inputText.getBytes(StandardCharsets.UTF_8))
    }
}

輸出:

I remembered black skies
The lightning all around me
I remembered each flash
As time began to blur
Like a startling sign
That fate had finally found me
And your voice was all I heard
That I get what I deserve
So give me reason
To prove me wrong
To wash this memory clean
Let the floods cross
The distance in your eyes

在上面的示例中,getBytes() 將字串編碼為 UTF-8 字符集。

示例 2:

如果輸入字串很小,我們可以將所有內容寫在一行中。

import java.nio.file.{Paths, Files}
import java.nio.charset.StandardCharsets

object MyObject {
    def main(args: Array[String]) {
        Files.write(Paths.get("C:\\Users\\srjsu\\OneDrive\\Desktop\\test.txt"), "HELLO WORLD!".getBytes(StandardCharsets.UTF_8))
    }
}

輸出:

HELLO WORLD!

執行程式後,上面的輸出將寫入兩個程式碼示例的 test.txt 檔案中。

作者: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

相關文章 - Scala File