I'm reading a file with the extension .p12, which contains special characters. I want to verify that io:fileReadString accurately returns the data from the file. That is,
string inputData = check io:fileReadString(inputFile);
check io:fileWriteString(outputFile, inputData);
string outputData = check io:fileReadString(outputFile);
test:assertEquals(inputData, outputData);
io:fileReadStringassumes you are reading a text file encoded withUTF-8encoding. Furthermore, it strips away the carriage return (\nor\r) from the end of the file, which means if you read a file usingio:fileReadStringand write it back even if it is valid UTF-8 it could still be different since it stripped away the carriage return at the end of the file. Also if the file in question has byte sequences that are not validUTF-8byte sequences it may write completely different values to the file. To better illustrate the point consider the example,Therefore if you are reading a file that is not valid UTF-8 or need to rewrite the exact content back you must use the
io:fileReadBytesread the file and write it back usingio:fileWriteBytes