FS API
chevron down
 

FS API

Interface: FileSystem

The File System API provides the ability to read and write files.

It is design to be compatible with the Node.js File System API.

import { readFileSync } from "fs";
let text = readFileSync("my_file.txt", "utf-8");
console.log(text);

Find out more about this API in the File System Guide

existsSync()

New in SDK 4.0

Test whether or not the given path exists and is accessible through the filesystem.

fs.existsSync(path: string)

Parameters

path: string

Name of path to check for existence.

writeFileSync()

Write data to a file in a single operation. If a file with that name already exists, it is overwritten; otherwise, a new file is created.

fs.writeFileSync(filename: string, data: ArrayBuffer | ArrayBufferView)

Parameters

filename: string

Name of the file to write.

data: ArrayBuffer or ArrayBufferView

With no encoding specified, data is a binary data buffer. With encoding == "ascii" or "utf-8", data is a string that will be serialized with the respective encoding. With encoding == "cbor" or "json", data is an object that will be serialized with CBOR or JSON encoding, respectively.

fs.writeFileSync(filename: string, data: string, encoding: "ascii" | "utf-8")

Parameters

filename: string
data: string
encoding: "ascii" or "utf-8"

fs.writeFileSync(filename: string, data: any, encoding: "cbor" | "json")

Parameters

filename: string
data: any
encoding: "cbor" or "json"

readFileSync()

Read an entire file into a buffer in a single operation.

fs.readFileSync(filename: string)

Parameters

filename: string

Name of the file to read.

fs.readFileSync(filename: string, encoding: "ascii" | "utf-8")

Parameters

filename: string
encoding: "ascii" or "utf-8"

fs.readFileSync(filename: string, encoding: "cbor" | "json")

Parameters

filename: string
encoding: "cbor" or "json"

renameSync()

Rename a file.

fs.renameSync(oldFilename: string, newFilename: string)

Parameters

oldFilename: string

Name of the file to be renamed.

newFilename: string

Name to which the filename should be changed.

unlinkSync()

Delete a file.

fs.unlinkSync(filename: string)

Parameters

filename: string

Name of the file to be deleted.

statSync()

Get metadata of a file.

fs.statSync(filename: string)

Parameters

filename: string

Name of the file of which the metadata is requested.

writeSync()

Write buffer to the file specified by fd.

fs.writeSync(fd: number, buffer: ArrayBuffer, offset?: number | undefined, length?: number | undefined, position?: number | undefined)

Parameters

fd: number

File descriptor to which to write.

buffer: ArrayBuffer

Buffer containing the data to write.

(optional) offset: number or undefined

Offset in buffer of the first byte of data to write. (default: 0)

(optional) length: number or undefined

Number of bytes to write. (default: the size of the buffer)

(optional) position: number or undefined

Offset from the beginning of the file where the data should be written. If the file was opened in append mode (a or a+), this parameter is ignored.

readSync()

Read data from the file specified by fd.

fs.readSync(fd: number, buffer: ArrayBuffer, offset?: number | undefined, length?: number | undefined, position?: number | undefined)

Parameters

fd: number

File descriptor from which to read.

buffer: ArrayBuffer

Buffer in which to store the read data.

(optional) offset: number or undefined

Offset in buffer of the first byte where the file data is to be stored. (default: 0)

(optional) length: number or undefined

Number of bytes to read. (default: the size of the buffer)

(optional) position: number or undefined

Offset from the beginning of the file from which the data should be read. (default: 0)

closeSync()

Close a file descriptor.

fs.closeSync(fd: number)

Parameters

fd: number

File descriptor for the file to close.

openSync()

Open a file.

Filenames are flat names without hierarchy and cannot contain path separators (/) or prefixes.

A filename is a non-empty string of up to 64 characters

Valid characters for filenames are: a-z, A-Z, 0-9, !, #, $, %, &, ', (, ), -, @, ^, _, {, }, ~, .,

The reserved names . and .. are not allowed as filenames.

fs.openSync(filename: string, flags: "r" | "r+" | "w" | "w+" | "a" | "a+")

Parameters

filename: string

Name of the file to open.

flags: "r" or "r+" or "w" or "w+" or "a" or "a+"

Flags that specify the mode with which to open the file.

  • r: Open the file for reading. An error is thrown if the file does not exist.
  • r+: Open the file for reading and writing. An error is thrown if the file does not exist.
  • w: Open the file for writing. The file is created if it does not exist or truncated if it does.
  • w+: Open the file for reading and writing. The file is created if it does not exist or truncated if it does.
  • a: Open the file for appending. The file is created if it does not exist.
  • a+: Open the file for reading and appending. The file is created if it does not exist.

listDirSync()

New in SDK 2.0

Method to initiate the process of non-recursively listing all items in a given directory.

fs.listDirSync(path: string)

Parameters

path: string

Path to read from.

Interface: DirectoryIteratorResult

DirectoryIteratorResult

Properties

readonly done

boolean

boolean indicating whether we are done iterating

readonly value

string or undefined

name of current directory entry or undefined if done is true

Interface: DirectoryIterator

Directory

Methods

next()

Returns: DirectoryIteratorResult

Retrieve the next directory entry

Interface: FileStats

File metadata

Properties

readonly mtime

Date

Time at which the file data was last modified.

readonly size

number

Size of the file in bytes.