Last active
May 25, 2024 10:19
-
-
Save dacr/fff35edd387021c16999875933e2a0c8 to your computer and use it in GitHub Desktop.
ZIO learning - playing with nio - read content from file / published by https://github.com/dacr/code-examples-manager #210b0a07-6be8-4cad-ab9e-954e82fa1d8d/fae82dfe275947ddd74e9e6663e75fd40a0b815
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : ZIO learning - playing with nio - read content from file | |
// keywords : scala, zio, learning, nio, file, charset, permissions, write, read, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 210b0a07-6be8-4cad-ab9e-954e82fa1d8d | |
// created-on : 2021-06-05T19:53:50Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio-nio:2.0.1" | |
// --------------------- | |
import zio.* | |
import zio.nio.file.* | |
import zio.nio.charset.Charset | |
import java.nio.file.attribute.{PosixFileAttributes, PosixFilePermission, PosixFilePermissions} | |
object App extends ZIOAppDefault { | |
val hello = "Hello world !\nYes it works" | |
val charset = Charset.Standard.utf16 | |
val logic = for { | |
encoded <- charset.encodeString(hello) | |
perms = PosixFilePermissions.fromString("rw-r--r--") | |
attrs = PosixFilePermissions.asFileAttribute(perms) | |
dest <- Files.createTempFile(prefix = Some("truc"), fileAttributes = attrs :: Nil) | |
_ <- Files.deleteIfExists(dest) | |
_ <- Files.writeBytes(dest, encoded) | |
bytesRead <- Files.readAllBytes(dest) | |
content <- charset.decodeString(bytesRead) | |
- <- Console.printLine(s"read from $dest") | |
- <- Console.printLine(content) | |
} yield () | |
override def run = logic | |
} | |
// ------------------------------------------------------------- | |
App.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment