node.js中的fs.truncateSync方法使用说明
方法说明:
语法:
fs.truncateSync(path,len)
由于该方法属于fs模块,使用前需要引入fs模块(varfs=require(“fs”))
接收参数:
path 文件路径
len 截断长度,只保留该字符长度内的字符,超出部分将被清除。
例子:
varfs=require("fs");
fs.truncateSync("126.txt",3);
源码:
fs.truncateSync=function(path,len){
if(util.isNumber(path)){
//legacy
returnfs.ftruncateSync(path,len);
}
if(util.isUndefined(len)){
len=0;
}
//allowerrortobethrown,butstillclosefd.
varfd=fs.openSync(path,"r+");
try{
varret=fs.ftruncateSync(fd,len);
}finally{
fs.closeSync(fd);
}
returnret;
};
相关文章