您可以在單一行中要求整個工具組
//> using toolkit latest
或者,您也可以只要求 OS-Lib 的特定版本
//> using dep com.lihaoyi::os-lib:0.9.1
在您的 build.sbt
中,您可以新增對工具組的相依性
lazy val example = project.in(file("example"))
.settings(
scalaVersion := "3.2.2",
libraryDependencies += "org.scala-lang" %% "toolkit" % "0.1.7"
)
或者,您也可以只要求 OS-Lib 的特定版本
libraryDependencies += "com.lihaoyi" %% "os-lib" % "0.9.1"
在您的 build.sc
檔案中,您可以新增對工具組的相依性
object example extends ScalaModule {
def scalaVersion = "3.2.2"
def ivyDeps =
Agg(
ivy"org.scala-lang::toolkit:0.1.7"
)
}
或者,您也可以只要求 OS-Lib 的特定版本
ivy"com.lihaoyi::os-lib:0.9.1"
一次撰寫一個檔案
os.write
將提供的字串寫入新檔案
val path: os.Path = os.temp.dir() / "output.txt"
os.write(path, "hello\nthere\n")
println(os.read.lines(path).size)
// prints: 2
覆寫或附加
os.write
檔案已存在時會擲回例外
os.write(path, "this will fail")
// this exception is thrown:
// java.nio.file.FileAlreadyExistsException
為避免此情況,請使用 os.write.over
取代現有內容。
您也可以使用 os.write.append
在結尾新增內容
os.write.append(path, "two more\nlines\n")
println(os.read.lines(path).size)
// prints: 4
此頁面的貢獻者
內容
- 簡介
- 使用 MUnit 進行測試
- 如何撰寫測試?
- 如何執行測試?
- 如何執行單一測試?
- 如何測試例外?
- 如何撰寫非同步測試?
- 如何管理測試資源?
- MUnit 還有什麼功能?
- 使用 OS-Lib 處理檔案和程序
- 如何讀取目錄?
- 如何讀取檔案?
- 如何撰寫檔案?
- 如何執行程序?
- OS-Lib 還有什麼功能?
- 使用 uPickle 處理 JSON
- 如何存取 JSON 內的值?
- 如何修改 JSON?
- 如何將 JSON 反序列化為物件?
- 如何將物件序列化為 JSON?
- 如何讀寫 JSON 檔案?
- uPickle 還有什麼功能?
- 使用 sttp 傳送 HTTP 要求
- 如何傳送要求?
- 如何建構 URI 和查詢參數?
- 如何傳送帶有主體的請求?
- 如何傳送和接收 JSON?
- 如何透過 HTTP 上傳檔案?
- sttp 還有什麼功能?