Home > Web Front-end > JS Tutorial > Code implementation of file directory operations in the file system in Node.js

Code implementation of file directory operations in the file system in Node.js

不言
Release: 2018-08-23 17:21:58
Original
1070 people have browsed it

The content of this article is about the code implementation of file directory operations in the file system in Node.js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

File/directory operations

(1) Determine whether the file/directory exists

fs.exists( )

## fs.access()

fs.exitesSync()

(2) Delete files/directories

                                                                                                                                           (3) Rename files/directories

##       fs.rename()

        fs.renameSync()

(4) View file/directory status

fs.stat()

fs.statSync()

                fs.stats class, details:

http://nodejs.cn/api/fs.html#fs_class_fs_stats

Example:Directory fs/fs-2.js:

// 导入模块
  const fs = require('fs');
  // 判断文件/目录 是否存在 access()
  fs.access('../fs/fs-1.js', function (err) {
     if (err) {
        console.log("文件或目录不存在!");
     } else {
        console.log("文件或目录存在!");
     }
  });
  // 重命名同上方法  删除
  // 查看文件或者目录的状态
  fs.stat('../fs/fs-1.js',function (err,stat) {
    console.log(stat.ctime);//创建时间2018-07-27T06:39:57.719Z
    console.log(stat.mtime);//修改时间2018-07-27T06:39:57.683Z
    console.log(stat.atime);//访问时间2018-07-27T06:39:57.682Z
    console.log(stat.isFile());//是否是文件 输出true
    console.log(stat.isDirectory());//是否是目录输出false
  })
Copy after login
Related recommendations:

The implementation of synchronization and asynchronousness of the file system in Node.js

Detailed introduction to global objects in Node.js

The above is the detailed content of Code implementation of file directory operations in the file system in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template