当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js fs.lstat()用法及代码示例


fs.lstat()方法与fs.stat()方法类似,不同之处在于它用于返回有关用于引用文件或目录的符号链接的信息。返回的fs.Stat对象具有多个字段和方法,以获取有关文件的更多详细信息。

用法:

fs.lstat( path, options, callback )

参数:此方法接受上述和以下所述的三个参数:



  • path:它是一个字符串,缓冲区或URL,其中包含符号链接的路径。
  • options:该对象可用于指定将影响输出的可选参数。它具有一个可选参数:
    • bigint:它是一个布尔值,它指定fs.Stats对象中返回的数值是否为bigint。默认值为false。
  • callback:执行该方法时将调用该函数。
    • err:如果使用该方法,则会引发错误。
    • Stats:Stats对象包含文件路径的详细信息。

以下示例说明了Node.js中的fs.lstat()方法:

范例1:本示例使用fs.lstat()方法获取文件符号链接的详细信息。

// Node.js program to demonstrate the 
// fs.lstat() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
fs.symlinkSync(__dirname + "\\example_file.txt", 
                        "symlinkToFile", 'file'); 
console.log("Symlink to file created") 
  
fs.lstat("symlinkToFile", (err, stats) => { 
  if (err) 
    console.log(err); 
  else { 
    console.log("Stat of symlinkToFile") 
    console.log(stats); 
  } 
});

输出:

Symlink to file created
Stat of symlinkToFile
Stats {
  dev:3229478529,
  mode:41398,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780939,
  size:45,
  blocks:0,
  atimeMs:1585207132017.4473,
  mtimeMs:1585207132017.4473,
  ctimeMs:1585207132017.4473,
  birthtimeMs:1585207132017.4473,
  atime:2020-03-26T07:18:52.017Z,
  mtime:2020-03-26T07:18:52.017Z,
  ctime:2020-03-26T07:18:52.017Z,
  birthtime:2020-03-26T07:18:52.017Z
}

范例2:本示例使用fs.lstat()方法获取指向文件夹的符号链接的详细信息。

// Node.js program to demonstrate the 
// fs.lstat() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
fs.symlinkSync(__dirname + "\\example_directory", 
                          "symlinkToDir", 'dir'); 
  
console.log("Symlink to directory created") 
  
fs.lstat("symlinkToDir", (err, stats) => { 
  if (err) 
    console.log(err); 
  else { 
    console.log("Stat of symlinkToDir") 
    console.log(stats); 
  } 
});

输出:

Symlink to directory created
Stat of symlinkToDir
Stats {
  dev:3229478529,
  mode:41398,
  nlink:1,
  uid:0,
  gid:0,
  rdev:0,
  blksize:4096,
  ino:281474976780940,
  size:46,
  blocks:0,
  atimeMs:1585207184224.7136,
  mtimeMs:1585207184224.7136,
  ctimeMs:1585207184224.7136,
  birthtimeMs:1585207184224.7136,
  atime:2020-03-26T07:19:44.225Z,
  mtime:2020-03-26T07:19:44.225Z,
  ctime:2020-03-26T07:19:44.225Z,
  birthtime:2020-03-26T07:19:44.225Z
}

参考: https://nodejs.org/api/fs.html#fs_fs_lstat_path_options_callback




相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.lstat() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。