NodeJs the file module

Foreword

Node.js file system (fs module), provides us create directories and files, delete, and read and write operations.

Method module are asynchronous and synchronous, for example, a function to read the contents of the file:
asynchronous fs.readFile ()
synchronization fs.readFileSync ().

Asynchronous method functions The last parameter is the callback function, the first parameter is a callback function error, the second is the successful return res, it is recommended that you use the asynchronous method, compared to synchronous, asynchronous method for higher performance, faster, and there is no obstruction .

这里的最后一个参数,大家请注意,就是调用函数的最后一个参数可以直接写回调函数,中间参数可忽略不计

fsModule has the following commonly used methods, other interception, closed, open; I will not listed here talk about several commonly used methods, 同步异步我就不做过多的解释,这里统一用异步方式去实践 .

// 这些方法的使用都是 fs.方法名()
readFile() readFileSync()  // 读文件 - 异步同步
writeFile() writeFileSync() // 写文件(创建文件) - 异步同步
unlink() unlinkSync() // 删除文件 - 异步同步
mkdir()  mkdirSync() // 创建目录  - 异步同步
readdir() readdirSync() // 读取目录 - 异步同步
rmdir() rmdirSync() // 删除目录 - 异步同步
exists(path, callback) // 判断path是否存在 也有同步和异步
stat() // 返回一个对象包含当前读取目录或者文件的内容及信息

注意:实例中的参数使用并不完全,我只是为了演示功能性的必要参数和回调,具体更多参数请参考官方文档

Read files
here to note the contents of the file returned is Bufferthe class of binary data stream, you need toString()only see really content

fs.readFile("./input.txt", function (err, res) {
  if (err)  throw err
  console.log(res)
  console.log(res.toString())
})
// PS C:\HiSen\myWorkDemo\node_demo> node .\fs.js
// <Buffer 68 65 6c 6c 6f 20 e5 9b 9e e8 b0 83 e5 87 bd e6 95 b0>
// hello 回调函数

Write file
write file first parameter pathpassed in the path if the content will overwrite the contents of the documents, and if not it will automatically create the file and write the second argument exists

fs.writeFile("./input.txt", "文件要写的内容", function (err) {
  if (err) throw err
})

Deleted files
do here delete the time it normally takes to judge whether a file exists, use the function exists, the back will write how to use it.

// 删除文件
fs.unlink("./input2.txt", function (err) {
  if (err) throw err
})

Create a directory
Note that the function of this method can not create multiple directories at the same time, such as: fs.mkdir("./A/B/C")Although the document has parameters are written recursively created recursiveto value trueit can create, but I tested, seemingly not, I might node version of the problem so? If you need to create multiple levels of directories recursively, you can write a recursive function calls. do not do the presentation, degree of your mother a lot here.

var fs = require("fs");
fs.mkdir("./nodeFs", function (err) {
  if (err) throw err
  console.log("创建成功")
})

Read the directory
read the directory, the result is an array of files in the directory folder and file name suffix, as an example, we nodeFscreate a folder f1 folder, two text files are 1.txt, 2.txt return results[ '1.txt', '2.txt', 'f1' ]

fs.readdir("./nodeFs", function (err, res) {
  if (err) throw err
  console.log(res) // [ '1.txt', '2.txt', 'f1' ]
})

Remove directory

fs.rmdir("./nodeFs2", function (err) {
  if (err) throw err
})

Determine pathwhether there is,exists
here pathit can be a directory can also be a file and returns a Boolean value, truethere is falsenot present

fs.exists("./nodeFs", function (exists) {
  console.log(exists) // true
})

stat
returns an object that contains the identification information of the current file or directory, the file is typically used to determine or folder, through isFile() or isDirectory()judged.

fs.stat("./input.txt", function (err, res) {
  if (err) throw err;
  console.log(res.isFile()) // true
  console.log(res.isDirectory())  // false
})

Returns the object information is as follows

PS C:\HiSen\myWorkDemo\node_demo> node .\fs.js
Stats {
  dev: 1279169449,
  mode: 33206,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: undefined,
  ino: 1688849861505325,
  size: 0,
  blocks: undefined,
  atimeMs: 1576909108592.982,
  mtimeMs: 1576909108592.982,
  ctimeMs: 1576909108592.982,
  birthtimeMs: 1575876788447.5103,
  atime: 2019-12-21T06:18:28.593Z,
  mtime: 2019-12-21T06:18:28.593Z,
  ctime: 2019-12-21T06:18:28.593Z,
  birthtime: 2019-12-09T07:33:08.448Z }
true
false

Some watchfile(),unwatchfile()monitor file changes and cancel functions triggered here I do not listen too much demonstration and practice, and everyone interested can try.

Published 20 original articles · won praise 40 · views 5865

Guess you like

Origin blog.csdn.net/HiSen_CSDN/article/details/103643040