以 UTF-8 格式读取文件。
文件路径。
读取选项,指定编码为 'utf8'。
包含文件内容的字符串的操作结果。
1.1.0
const result = readFileSync('/path/to/file.txt', { encoding: 'utf8' });if (result.isOk()) { console.log(result.unwrap());} Copy
const result = readFileSync('/path/to/file.txt', { encoding: 'utf8' });if (result.isOk()) { console.log(result.unwrap());}
以二进制格式读取文件。
Optional
读取选项,指定编码为 'bytes'。
包含文件内容的 Uint8Array 的操作结果。
const result = readFileSync('/path/to/file.txt', { encoding: 'bytes' });if (result.isOk()) { const bytes = result.unwrap(); console.log(decodeUtf8(bytes));} Copy
const result = readFileSync('/path/to/file.txt', { encoding: 'bytes' });if (result.isOk()) { const bytes = result.unwrap(); console.log(decodeUtf8(bytes));}
readFile 的同步版本,读取文件内容。
readFile
文件的路径。
可选的读取选项。
包含文件内容的操作结果。
const result = readFileSync('/path/to/file.txt');if (result.isOk()) { const bytes = result.unwrap(); console.log(decodeUtf8(bytes));} Copy
const result = readFileSync('/path/to/file.txt');if (result.isOk()) { const bytes = result.unwrap(); console.log(decodeUtf8(bytes));}
以 UTF-8 格式读取文件。