Argument | Description | Types / Required |
path | The path of the file to write. | string
required |
bytes | The bytes to write. | string
required |
bytesFormat | The format of bytes.
The default value is "autodetect". | string
not required |
The
File.writeBytes action writes
bytes to a binary file specified as
path. The optional
bytesFormat argument dictates how
binary data is converted from string format. If a file already exists at
path, it is overwritten with the new contents.
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 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));