Home > Web Front-end > JS Tutorial > body text

About the node.js method of reading and writing system files and directories based on the fs module

不言
Release: 2018-06-30 14:12:54
Original
1635 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 nodejs using the fs module to read, write, create, delete and other related operations of files and directories in the form of examples. For tips, friends who need them can refer to

This article describes how node.js reads and writes system files and directories based on the fs module. Share it with everyone for your reference, the details are as follows:

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 readFile method using

fs.readFile(filename,[option],callback) method 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 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 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 is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to Node.js connecting to postgreSQL and performing data operations

##nodejs uses ztree to implement Move between two divs

The above is the detailed content of About the node.js method of reading and writing system files and directories based on the fs module. For more information, please follow other related articles on the PHP Chinese website!

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