node uses: fs module

The following are the common methods of fs module. For more details, please refer to the official website: http://nodejs.cn/api/fs.html

// fs is the core module of node, no need to install
const fs = require('fs');

fs.access(path[, mode], callback)

/** 
 * Test the user's permissions on the file or directory specified by path.
 * @param {String|Buffer} path 
 * @param {Integer} mode Default value: fs.constants.f'ok
 * @param {Function} callback
 */
 
const file = 'package.json';

// Check if the file exists in the current directory.
fs.access(file, fs.constants.F_OK, (err) => {
  console.log(`${file} ${err ? 'Non-existent' : 'existence'}`);
});

// Check that the file is readable.
fs.access(file, fs.constants.R_OK, (err) => {
  console.log(`${file} ${err ? 'unreadable' : 'readable'}`);
});

// Check that the file is writable.
fs.access(file, fs.constants.W_OK, (err) => {
  console.log(`${file} ${err ? 'Not to write' : 'Writable'}`);
});

// Check that the file exists in the current directory and that it is writable.
fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
  if (err) {
    console.error(
      `${file} ${err.code === 'ENOENT' ? 'Non-existent' : 'Readable only'}`);
  } else {
    console.log(`${file} Exists, and it is writable`);
  }
});

It is not recommended to use fs.access() to check file accessibility before calling fs.open(), fs.readFile(), or fs.writeFile() Doing so introduces race conditions because other processes may change the state of the file between calls Instead, you should open, read, or write to the file directly, and if the file is inaccessible, handle the errors that are raised.

fs.open(path, flags[, mode], callback)

/** 
 *Open the file asynchronously.
 * @param {String|Buffer} path 
 *@ param {String|Number} flags see supported file system flags.
 *@ param {integer} mode 0o666 (read-write).
 * @param {Function} callback
 * 		err 
 * 		fd 
 */

fs.readFile(path[, options], callback)

/** 
 * Reads the entire contents of the file asynchronously.
 * @param {String|Buffer} path File name or file descriptor.
 * @param {Object|String} options
 * 		encoding Default: null
 * 		flag See supported file system flags. Default: 'r'
 * @param {Function} callback
 * 		err 
 * 		data Contents of the document
 */
 
fs.readFile('/etc/passwd', (err, data) => {
    if (err) throw err;
    console.log(data);
});

fs.writeFile(file, data[, options], callback)

/** 
 * Writes data asynchronously to a file, overwriting it if it already exists.
 * @param {String|Buffer|URL|integer} file File name or file descriptor.
 * @param {String|Buffer|TypedArray|DataView} data
 * @param {Object|String} options
 * 		encoding Default: null
 * 		mode Default: 0o666
 * 		flag See supported file system flags. Default: 'r'
 * @param {Function} callback
 * 		err 
 */

const data = new Uint8Array(Buffer.from('Node.js Chinese net'));
fs.writeFile('file.txt', data, (err) => {
    if (err) throw err;
    console.log('File saved');
});

fs.appendFile(path, data[, options], callback)

/** 
 * Asynchronously appends data to a file, which is created if it does not already exist Data can be string or Buffer
 * @param {String|Buffer} path Filename or file descriptor
 * @param {String|Buffer} data
 * @param {Object|String} options
 * 		encoding Default: 'utf8'
 * 		mode Default: 0o666
 * 		flag See supported file system flags. Default: 'a'
 * @param {Function} callback
 */
 
fs.appendFile('message.txt', 'Additional data', (err) => {
  if (err) throw err;
  console.log('Data appended to file');
});

// If options is a string, it specifies the character encoding:
fs.appendFile('message.txt', 'Additional data', 'utf8', callback);

// path can be specified as an open numeric file descriptor for appending (using fs.open() or fs.openSync()) The file descriptor does not close automatically.
fs.open('message.txt', 'a', (err, fd) => {
    if (err) throw err;
    fs.appendFile(fd, 'Additional data', 'utf8', (err) => {
        fs.close(fd, (err) => {
            if (err) throw err;
        });
        if (err) throw err;
    });
});

fs.mkdir(path[, options], callback)

/** 
 * Create directories asynchronously The completion callback has no parameters other than possible exceptions.
 * @param {String|Buffer} path 
 * @param {Object|integer} options
 * 		recursive Should parent folder be created default: false
 * 		mode Default: 0o777
 * @param {Function} callback
 */
 
// Create the / tmp/a/apple directory, regardless of whether the / tmp and / tmp/a directories exist.
fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => {
  if (err) throw err;
});

Tags: Front-end encoding JSON

Posted on Thu, 07 Nov 2019 08:42:25 -0800 by blanius