zoukankan      html  css  js  c++  java
  • nodejs 判断文件属性-目录或者文件

    一、查看文件属性

    文档参考地址

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

    const fs = require('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('这是一个文件')
        }
    })
    
  • 相关阅读:
    线程安全的简单理解
    单链表 之 判断两链表是否交叉
    React组件间的通信
    input type=file美化
    Array.prototype.slice.call(arguments)
    ES5 数组方法every和some
    nodejs学习之表单提交(1)
    ES5 数组方法reduce
    ES5 数组方法map
    ES5 数组方法forEach
  • 原文地址:https://www.cnblogs.com/linjunfu/p/12909411.html
Copyright © 2011-2022 走看看