Import std.io.file_stream
FileStream
class
Public Class FileStream Implements Reader, Writer, Seeker
A file opened to read, write, or both, at a place in it that each read and write moves on, and Seek moves anywhere, until it's closed, or its last reference goes. It holds nothing back, so Flush has nothing to do, and Sync puts what's been written onto the disk. A BufferedReader or a BufferedWriter around it takes fewer calls for small reads and writes. One call at a time goes on it: a call while an Async one is under way panics, and so does using it once it's closed, reading one not opened to read, and writing one not opened to write.
Examples
Local folder = Try TempDir()
Local path = folder.Path / "greeting.txt"
Local stream = Try File.Create(path)
Try stream.WriteText("hello, world")
Try stream.Seek(7, SeekOrigin.START)
Try stream.WriteText("there")
stream.Close()
Expect((Try File.ReadText(path)) == "hello, there")
Try folder.Close()std/io/file_stream.fb, line 109
-
The file it's open on.
-
Whether it was opened to read.
-
Whether it was opened to write.
-
Whether each write goes at the end.
-
Whether it's still open.
-
Reads up to into.Count bytes from where it is, and moves on past them.
-
Read's Async twin, which libuv's thread pool does while the main thread goes on.
-
Writes all of from's bytes where it is, or at the end when it appends, and moves on past them.
-
Write's Async twin, which libuv's thread pool does while the main thread goes on.
-
Holds nothing back, so there's nothing to send on, but it panics once it's closed too.
-
Flush's Async twin, which has nothing to send on either, but panics once it's closed too.
-
Puts what's been written to the file onto the disk, as File's atomic writes do theirs, so it's there even if the machine stops. One not opened to write has nothing to put there.
-
What Sync does, without blocking its thread: a call gives its Task.
-
@PanicsAtCaller Public Method Seek(offset: Int64, origin: SeekOrigin): Int64 Throws IOErrormethod #Moves to
offsetbytes fromorigin, as Seeker says, where the next read reads and the next write writes, unless it appends. A place past the end is where a write makes the file longer, with 0s before it. -
How many bytes the file has now.
-
Closes the file, which a second Close leaves alone. What was written stays in the file, for the OS to put onto the disk.
Report an issue with this page or with FutureBlitz on GitHub.