码迷,mamicode.com
首页 > Web开发 > 详细

nodejs 判断文件属性-目录或者文件

时间:2020-05-18 12:44:25      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:文件权限   lock   lse   direct   默认值   函数   权限   string   callback   

一、查看文件属性

文档参考地址

1. 常用查看文件属性方式

const fs = requiere(‘fs‘)
const path = require(‘path‘)
let fileName1 = path.resolve(__dirname, ‘./fileName1.txt‘)
let dirName1 = path.resolve(__dirname, ‘./dirName1‘)
// 异步查看文件属性
fs.stat(fileName1, (err, stats) => {
    if (err) throw err
    console.log(‘文件属性:‘, stats)
})

fs.stat(dirName1, (err, stats) => {
    if (err) throw err
    console.log(‘文件属性:‘, stats)
})

2. 语法说明

/*
 * 异步查看文件属性
 * @param path {string | Buffer | URL |} 目录名
 * @param options {Object | integer}
 *          bigint {boolean}, 返回的 fs.stats 对象中的数值是否为 bigint 型,默认值为 false
 * @param callback {Function} 回调函数
 *          err {Error} 查看文件属性时抛出的错误
 *          stats {fs.Stats} 文件属性对象
 */
fs.stat(path[, options], callback)

备注:

  1. 回调函数返回的 stats 是一个 fs.Stats 对象
const fs = require(‘fs‘)
const path = require(‘path‘)

let dirName = path.resolve(__dirname, ‘../fs‘)

fs.stat(dirName, (err, stats) => {
    console.log(‘stats:>> ‘, stats)
    /*
    Stats {
        dev: 2483030547,
        mode: 16822,
        nlink: 1,
        uid: 0,
        gid: 0,
        rdev: 0,
        blksize: undefined,
        ino: 48695170970944296,
        size: 0,
        blocks: undefined,
        atimeMs: 1589769153580.5305,
        mtimeMs: 1589769153580.5305,
        ctimeMs: 1589769153580.5305,
        birthtimeMs: 1589768320730.911,
        atime: 2020-05-18T02:32:33.581Z, // 读取文件或者执行文件时候更改
        mtime: 2020-05-18T02:32:33.581Z, // 写入文件随文件内容的更改而更改
        ctime: 2020-05-18T02:32:33.581Z, // 写入文件,更改文件所有者,更改文件权限或者链接设置时更改
        birthtime: 2020-05-18T02:18:40.731Z // 创建时间
    }
    */
})
  1. options 中的 biginttrue 时,数值是 bigint 型而不是 number
const fs = require(‘fs‘)
const path = require(‘path‘)

let dirName = path.resolve(__dirname, ‘../fs‘)

fs.stat(dirName, { bigint: true }, (err, stats) => {
    console.log(‘stats:>> ‘, stats)
   /*
    
    Stats {
        dev: 2483030547n,
        mode: 16822n,
        nlink: 1n,
        uid: 0n,
        gid: 0n,
        rdev: 0n,
        blksize: undefined,
        ino: 48695170970944296n,
        size: 0n,
        blocks: undefined,
        atimeMs: 1589769153580n,
        mtimeMs: 1589769153580n,
        ctimeMs: 1589769153580n,
        birthtimeMs: 1589768320730n,
        atime: 2020-05-18T02:32:33.580Z,
        mtime: 2020-05-18T02:32:33.580Z,
        ctime: 2020-05-18T02:32:33.580Z,
        birthtime: 2020-05-18T02:18:40.730Z 
    }
    */
})

二、同步查看文件属性

功能和参数与该接口的 异步 API 类似,只是参数少了 回调函数

const fs = require(‘fs‘)
fs.statSync(path[, options])

三、判断目录或者文件

const fs = require(‘fs‘)
const path = require(‘path‘)

let dirName = path.resolve(__dirname, ‘../fs‘)
let fileName = path.resolve(__dirname, ‘./nodejs 写入文件.md‘)

// 判断目录
fs.stat(dirName, (err, stats) => {
    if (err) throw err
    if (stats.isDirectory()) {
        console.log(‘这是一个目录‘)
    }
})

// 判断文件
fs.stat(fileName, (err, stats) => {
    if (err) throw err
    if (stats.isFile()) {
        console.log(‘这是一个文件‘)
    }
})

nodejs 判断文件属性-目录或者文件

标签:文件权限   lock   lse   direct   默认值   函数   权限   string   callback   

原文地址:https://www.cnblogs.com/linjunfu/p/12909411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!