Argument | Description | Types / Required |
path | The path of the file to write. | string
required |
lines | The array of strings to write. | array
required |
encoding | The encoding to use while writing the text.
The default value is "UTF-8". | string
not required |
The
File.writeLines action writes an array of strings,
lines, as lines to a text file specified as
path. After writing each line, the action writes a newline character:
'\n'. If a file already exists at
path, it is overwritten with the new contents.
The
encoding argument allows you specify how the contents of the file are
encoded to text. Options include:
- "ANSI": The contents are encoded as part of the Windows code page.
- "UTF-8": The contents are encoded as UTF-8 and written without a byte order mark (BOM).
- "UTF-8-BOM": The contents are encoded as UTF-8 and written with a three-byte BOM.
The action returns undefined.
The action throws an exception if any of its arguments are not specified in a valid form or if the file cannot be created or fully written.
// read all the lines from a file
CS.File.readLinesAsync({
path: "text-file.txt"
})
.then(lines => {
// remove whitespace from the end of each line
const trimmedLines = lines.map(line => line.trimEnd());
// write the lines
return CS.File.writeLinesAsync({
path: "text-file (trimmed).txt",
lines: trimmedLines
});
})
.then(() => console.log("Successfully read, trimmed, and wrote the lines."))
.catch(error => console.log(error));