File Example

Common File Operations Including Creation, Deletion, Reading, Writing, and Closing

Code:

import std.fs.*
import std.io.SeekPosition

main() {
    let filePath: Path = Path("./tempFile.txt")
    if (File.exists(filePath)) {
        File.delete(filePath)
    }

    /* Creates a new file **'tempFile.txt'** in the current directory in write-only mode, writes "123456789\n" to the file three times, and closes the file. */
    var file: File = File(filePath, OpenOption.Create(false))
    if (File.exists(filePath)) {
        println("The file 'tempFile.txt' is created successfully in current directory.\n")
    }
    let bytes: Array<Byte> = "123456789\n".toArray()
    for (_ in 0..3) {
        file.write(bytes)
    }
    file.close()

    /* Opens the **'./tempFile.txt'** file in append mode, writes "abcdefghi\n" to the file, and closes the file. */
    file = File(filePath, OpenOption.Append)
    file.write("abcdefghi\n".toArray())
    file.close()

    /* Opens the **'./tempFile.txt'** file in read-only mode, reads data in the file as required, and closes the file. */
    file = File(filePath, OpenOption.Open(true, false))
    let bytesBuf: Array<Byte> = Array<Byte>(10, item: 0)
    // Reads a piece of data of 10 bytes after the 10th byte in the file header.
    file.seek(SeekPosition.Begin(10))
    file.read(bytesBuf)
    println("Data of the 10th byte after the 10th byte: ${String.fromUtf8(bytesBuf)}")
    // Reads a piece of data of 10 bytes in the file trailer.
    file.seek(SeekPosition.End(-10))
    file.read(bytesBuf)
    println("Data of the last 10 bytes: ${String.fromUtf8(bytesBuf)}")
    file.close()

    /* Opens the **'./tempFile.txt'** file in truncation mode, writes "The file was truncated to an empty file!" to the file, and closes the file. */
    file = File(filePath, OpenOption.Truncate(true))
    file.write("The file was truncated to an empty file!".toArray())
    file.seek(SeekPosition.Begin(0))
    let allBytes: Array<Byte> = file.readToEnd()
    file.close()
    println("Data written newly: ${String.fromUtf8(allBytes)}")

    File.delete(filePath)
    return 0
}

Running result:

The file 'tempFile.txt' is created successfully in current directory.

Data of the 10th byte after the 10th byte: 123456789

Data of the last 10 bytes: abcdefghi

Data written newly: The file was truncated to an empty file!

Demonstration of static Functions of File

Code:

import std.fs.*

main() {
    let filePath: Path = Path("./tempFile.txt")
    if (File.exists(filePath)) {
        File.delete(filePath)
    }

    /* Creates a file in write-only mode, writes "123456789\n" to the file, and closes the file. */
    var file: File = File.create(filePath)
    file.write("123456789\n".toArray())
    file.close()

    /* Writes "abcdefghi\n" to the file in append mode. */
    File.writeTo(filePath, "abcdefghi".toArray(), openOption: OpenOption.Append)

    /* Directly reads all data in the file. */
    let allBytes: Array<Byte> = File.readFrom(filePath)
    println(String.fromUtf8(allBytes))

    File.delete(filePath)
    return 0
}

Running result:

123456789
abcdefghi