Giter Site home page Giter Site logo

file_streams's Introduction

Gleam File Streams

This Gleam library provides access to Erlang's file streams for reading and writing files. If you don't require streaming behavior then consider using simplifile instead.

This library only supports the Erlang target.

Package Version Hex Docs

Example

Add this library to your project:

gleam add file_streams

The following code writes data to a file using a write stream, reads it back in using a binary file stream, then reads it again with a text file stream.

import file_streams/read_stream
import file_streams/read_stream_error
import file_streams/read_text_stream
import file_streams/write_stream
import gleam/bit_array

// Write file
let assert Ok(ws) = write_stream.open("test.txt")
let assert Ok(Nil) = write_stream.write_string(ws, "Hello, world!\n")
let assert Ok(Nil) = write_stream.write_bytes(ws, <<"12":utf8>>)
let assert Ok(Nil) = write_stream.close(ws)

// Read file as a binary stream
let assert Ok(rs) = read_stream.open("test.txt")

let assert Ok(bytes) = read_stream.read_bytes(rs, 14)
let assert Ok("Hello, world!\n") = bit_array.to_string(bytes)
let assert Ok([49, 50]) = read_stream.read_list(rs, read_stream.read_uint8, 2)
let assert Error(read_stream_error.EndOfStream) = read_stream.read_bytes(rs, 1)

let assert Ok(Nil) = read_stream.close(rs)

// Read file as a text stream so UTF-8 characters and lines can be read
let assert Ok(rts) = read_text_stream.open("test.txt")

let assert Ok("Hello") = read_text_stream.read_chars(rts, 5)
let assert Ok(", world!\n") = read_text_stream.read_line(rts)
let assert Ok("12") = read_text_stream.read_line(rts)
let assert Error(read_stream_error.EndOfStream) = read_text_stream.read_line(rts)

let assert Ok(Nil) = read_text_stream.close(rts)

Further documentation can be found at https://hexdocs.pm/file_streams/.

License

This library is published under the MIT license, a copy of which is included.

file_streams's People

Contributors

richard-viney avatar michael-mark-edu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.