if(err) throw err;
console.log('append成功');
});
文件内容截取
fs.truncate(path, len, callback) fs.truncateSync(path, len)
fs.ftruncate(fd, len, callback) fs.ftruncateSync(fd, len)
用途参考linux说明文档。
要点:
offset不会变化。比如通过fs.read()读取文件内容,就需要特别注意。
如果len小于文件内容长度,剩余文件内容部分会丢失;如果len大于文件内容长度,那么超出的部分,会用 进行填充。
如果传的是文件路径,需要确保文件是可写的;如果传的是文件句柄,需要确保文件句柄已经打开并且可写入。
The truncate() and ftruncate() functions cause the regular file named by path or referenced by fd to be truncated to a size of precisely length bytes.
If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes (‘
