Last active
May 6, 2023 15:40
-
-
Save dacr/3fb3146a5db9d120b3fd661f2f5f7821 to your computer and use it in GitHub Desktop.
ZIO learning - playing with nio - write to file / published by https://github.com/dacr/code-examples-manager #44fc4726-40f1-4ebe-9d31-61db07aa25f0/2ec31cff3ed8ebe1f65101c64de37b417c9dc4e6
This file contains 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 - write to file | |
// keywords : scala, zio, learning, nio, file, charset, permissions, write, 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 : 44fc4726-40f1-4ebe-9d31-61db07aa25f0 | |
// created-on : 2021-04-06T17:34:35Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.2.2" | |
//> using dep "dev.zio::zio-nio:2.0.1" | |
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0" | |
// --------------------- | |
import zio.*, zio.worksheet.* | |
import zio.nio.file.* | |
import zio.nio.charset.Charset | |
import java.nio.file.attribute.PosixFilePermissions | |
object App extends ZIOAppDefault { | |
val hello = "Hello world !" | |
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.writeBytes(dest, encoded) | |
_ <- Console.printLine(s"written to $dest !") | |
} 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