Detailed explanation of how node.js reads and writes system files and directories based on the fs module

小云云
Release: 2023-03-17 20:06:02
Original
2035 people have browsed it

This article mainly introduces the method of node.js to read and write system files and directories based on the fs module. It analyzes the related operation skills of nodejs using the fs module to read, write, create, and delete files and directories based on examples. Friends in need can refer to it.

If you want to use this module, you need to import it first. fs is already a module that comes with node.js, so you can import it directly


var fs = require('fs');
Copy after login


1. Read the file using the readFile method. Use the

fs.readFile(filename,[option],callback) method to read the file.

Parameter description:

filename String file name
option Object
encoding String |null default=null
flag String default='r'
callback Function


// 设置编码格式
fs.readFile('./test.txt', 'utf-8', function(err, data) {
  // 读取文件失败/错误
  if (err) {
    throw err;
  }
  // 读取文件成功
  console.log('utf-8: ', data.toString());
});
Copy after login


2. Write the file using the writeFile method

Use fs.writeFile(filename,data,[options],callback)Write content to the file.

Parameter description:

filename String file name
data String|buffer
option Object
encoding String |nulldefault='utf-8'
mode Number default=438(aka 0666 in Octal)
flag Stringdefault='w'
callback Function


var fs = require('fs'); // 引入fs模块
// 写入文件内容(如果文件不存在会创建一个文件)
// 写入时会先清空文件
fs.writeFile('./test2.txt', 'test test', function(err) {
  if (err) {
    throw err;
  }
  console.log('Saved.');
  // 写入成功后读取测试
  fs.readFile('./test2.txt', 'utf-8', function(err, data) {
    if (err) {
      throw err;
    }
    console.log(data);
  });
});
Copy after login


Because the default flag='w' is writing, the file will be cleared. If you want to append, you can pass a flag parameter, as follows.

flag passes the value, r represents reading the file, w represents writing the file, and a represents appending.


var fs = require('fs'); // 引入fs模块
// 写入文件内容(如果文件不存在会创建一个文件)
// 传递了追加参数 { 'flag': 'a' }
fs.writeFile('./test2.txt', 'test test', { 'flag': 'a' }, function(err) {
  if (err) {
    throw err;
  }
  console.log('Saved.');
  // 写入成功后读取测试
  fs.readFile('./test2.txt', 'utf-8', function(err, data) {
    if (err) {
      throw err;
    }
    console.log(data);
  });
});
Copy after login


3. Create directory mkdir using

Use fs.mkdir (path,[mode],callback)Create a directory, path is the directory that needs to be created, [mode] is the permission of the directory (default is 0777), callback is the callback function.


var fs = require('fs'); // 引入fs模块
// 创建 newdir 目录
fs.mkdir('./newdir', function(err) {
  if (err) {
    throw err;
  }
  console.log('make dir success.');
});
Copy after login


4. Read the directory readdir


var fs = require('fs'); // 引入fs模块
fs.readdir('./newdir', function(err, files) {
  if (err) {
    throw err;
  }
  // files是一个数组
  // 每个元素是此目录下的文件或文件夹的名称
  console.log(files);
});
Copy after login


5. Delete the file unlink


var fs = require('fs'); // 引入fs模块
fs.unlink('./newfile.txt', function(err) {
  if (err) {
    console.log(err);
    return false;
  }
  console.log("success");
});
Copy after login


6. Delete the empty folder rmdir


##

var fs = require('fs'); // 引入fs模块
fs.rmdir('./newdir', function(err) {
  if (err) {
    console.log(err);
    return false;
  }
  console.log("success");
});
Copy after login
The above content is a detailed explanation of how node.js reads and writes system files and directories based on the fs module. I hope Can help everyone.

How to implement a simple Node.js scaffolding

PHP and Node.js

node.js Code case sharing for operating MongoDB

The above is the detailed content of Detailed explanation of how node.js reads and writes system files and directories based on the fs module. 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