Argument | Description | Types / Required |
path | The path of the file to read. | string
required |
bytesFormat | The format in which the bytes are returned.
The default value is "dataUrl". | string
not required |
The
File.readBytes action reads a file, specified as
path, in binary format, returning the entire file's content as bytes. The argument
bytesFormat allows you to specify the
format of the returned binary data.
The action returns the file's content in bytes, represented as a string in one of the
output formats used for binary data.
The action throws an exception if any of its arguments are not specified in a valid form or if the file does not exist or cannot be read.
// read all the bytes from a file
CS.File.readBytesAsync({
path: "legacy.amo",
bytesFormat: "hex"
})
.then(hexBytes => {
// remove every other byte, represented in hexadecimal format (two characters per byte), from the file
let removeNextByte = false;
for( let i = 0; i < hexBytes.length; ) {
if( removeNextByte ) {
hexBytes = hexBytes.substring(0, i) + hexBytes.substr(i + 2);
removeNextByte = false;
}
else {
removeNextByte = true;
i += 2;
}
}
// write the bytes
return CS.File.writeBytesAsync({
path: "legacy (every-other-byte).amo",
bytes: hexBytes,
bytesFormat: "hex"
});
})
.then(() => console.log("Successfully read, removed half the content, and wrote the bytes."))
.catch(error => console.log(error));