File I/O

SCL provides several modules for reading and writing files. All file operations carry the <Proc> effect.

StringIO — line-by-line text files

StringIO is the simplest module for reading and writing text files line by line:

import "StringIO"

// Read all lines of a file into a list of strings
lines :: <Proc> [String]
lines = readLines "c:/data/input.txt"

// Write a list of strings to a file (one string per line)
writeLines "c:/data/output.txt" ["line 1", "line 2", "line 3"]

// Append lines to an existing file
appendLines "c:/data/log.txt" ["new entry"]
readLines :: String -> <Proc> [String] (StringIO)

readLines "filename" reads all lines of the file specified by fileName. The file contents are expected to be UTF-8 encoded.

writeLines :: String -> [String] -> <Proc> () (StringIO)

writeLines "fileName" lines creates new or overwrites existing file specified by fileName with the specified list of strings written as lines, i.e. separated by '\n' (line feed). The written text will be UTF-8 encoded.

File — basic file operations

The File module provides general file and directory operations using a File data type:

import "File"

// Create a File reference
f = file "c:/data/report.txt"

// Test existence
if fileExists f
then print "exists"
else print "not found"

// Read entire file as a string
content = readFile f

// Write a string to a file (overwrites)
writeFile f "Hello, world!\n"

// Copy and move
copyFile f (file "c:/backup/report.txt")
moveFile f (file "c:/archive/report.txt")

// Delete
deleteFile f

// List directory contents
entries = listDirectory (file "c:/data")

Files — advanced path-based operations

The Files module is a more comprehensive alternative that uses a Path data type (backed by java.nio.file.Path):

import "Files"

p = path "c:/data/report.txt"

content = readTextFile p
writeTextFile p "Hello"
exists = pathExists p
allFiles = walkFileTree (path "c:/data")

Path supports relative paths, path composition with /, and platform-agnostic separator handling.

Stream — binary I/O

The Stream module exposes InputStream and OutputStream for binary data:

import "Stream"

// Open an input stream from a file
withInputStream "c:/data/binary.bin" $ \is ->
    // read bytes from is
    ...

// Open an output stream to a file
withOutputStream "c:/data/out.bin" $ \os ->
    // write bytes to os
    ...

Use Stream when you need byte-level control or when working with non-text files.

Serialization — binary object I/O

The Serialization module supports reading and writing SCL values as binary data via streams:

import "Serialization"

// Write a value to a stream
serialize outputStream myValue

// Read a value back from a stream
myValue = deserialize inputStream

The binary format is specific to SCL's serialization protocol; it is not interchangeable with Java serialization by default.

Path separator notes

On Windows, \ is the native path separator, but it is also the escape character in SCL strings. Use one of these forms:

"c:/data/file.txt"      // forward slashes work on all platforms
"c:\\data\\file.txt"    // escaped backslashes
"""c:\data\file.txt"""  // triple-quoted strings: no escape processing