Last active
May 25, 2024 10:18
-
-
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/58e2ebb738345e5ade1085c033513d125a0f0235
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 - 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.4.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