Home  >  Article  >  Web Front-end  >  node.js中的fs.ftruncate方法使用说明_node.js

node.js中的fs.ftruncate方法使用说明_node.js

WBOY
WBOYOriginal
2016-05-16 16:27:021646browse

方法说明:

文件内容截取操作。

语法:

复制代码 代码如下:

fs.ftruncate(fd, len, [callback(err)])

由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) )

接收参数:

path           文件路径

len              截断长度,只保留该字符长度内的字符,超出部分将被清除。

callback      回调,传递一个异常参数err

例子:

复制代码 代码如下:

var fs = require('fs');
fs.ftruncate('126.txt', 2, function(err){
 if(err){
  throw err;
 }
 console.log('文件内容截断成功');
})

源码:

复制代码 代码如下:

fs.ftruncate = function(fd, len, callback) {
  if (util.isFunction(len)) {
    callback = len;
    len = 0;
  } else if (util.isUndefined(len)) {
    len = 0;
  }
  binding.ftruncate(fd, len, makeCallback(callback));
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn