


About the node.js method of reading and writing system files and directories based on the fs module
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');
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()); });
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); }); });
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); }); });
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.'); });
4. Read the directory readdir
var fs = require('fs'); // 引入fs模块 fs.readdir('./newdir', function(err, files) { if (err) { throw err; } // files是一个数组 // 每个元素是此目录下的文件或文件夹的名称 console.log(files); });
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"); });
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"); });
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is a language widely used in web development. It provides many functions and methods for processing files. In PHP, we can use binary mode to read and write files. This method can improve the efficiency of file operations, especially when processing binary files. In this article, we will explore binary file reading and writing operations in PHP and how to use this method to process binary files. What is a binary file? Binary files refer to files represented by pure binary, and their contents may contain different encoded character sets.

Use Java's File.isDirectory() function to determine whether a file exists and is of directory type. In Java programming, you often encounter situations where you need to determine whether a file exists and is of directory type. Java provides the File class to operate files and directories. The isDirectory() function can help us determine whether a file is a directory type. The File.isDirectory() function is a method in the File class. Its function is to determine the current File

PHP function introduction—rename(): Renaming files or directories Introduction: In PHP, the rename() function is used to rename files or directories. It provides an easy way to change the name of a file or directory. Whether it is a single file or an entire directory, you can use this function to perform a rename operation. The renaming process can be easily accomplished by specifying the name of the source file or directory and the target name. Syntax: boolrename(string$source,str

Use the path/filepath.Dir function to obtain the directory part of the file path. In our daily development process, file path processing is often involved. Sometimes, we need to get the directory part of the file path, that is, the path to the folder where the file is located. In the Go language, you can use the Dir function provided by the path/filepath package to implement this function. The signature of the Dir function is as follows: funcDir(pathstring)string The Dir function receives a word

The glob() function in PHP is used to find files or directories and is a powerful file operation function. It can return the path of a file or directory based on a specified pattern match. The syntax of the glob() function is as follows: glob(pattern, flags) where pattern represents the pattern string to be matched, which can be a wildcard expression, such as *.txt (matching files ending with .txt), or a specific file path. flags is an optional parameter used to control the function

Select the style of the catalog in Word, and it will be automatically generated after the operation is completed. Analysis 1. Go to Word on your computer and click to import. 2After entering, click on the file directory. 3 Then select the style of the directory. 4. After the operation is completed, you can see that the file directory is automatically generated. Supplement: The table of contents of the summary/notes article is automatically generated, including first-level headings, second-level headings and third-level headings, usually no more than third-level headings.

The mobile version of WeChat Reading App is a very good reading software. This software provides a lot of books. You can read them anytime, anywhere with just one click to search and read them online. All of them are officially authorized and different types of books are neatly arranged. Sort and enjoy a comfortable and relaxing reading atmosphere. Switch the reading modes of different scenarios, update the latest book chapters continuously every day, support online login from multiple devices, and batch download to the bookshelf. You can read it with or without the Internet, so that everyone can discover more knowledge from it. Now the editor details it online Promote the method of viewing the catalog for WeChat reading partners. 1. Open the book you want to view the catalog and click in the middle of the book. 2. Click the three lines icon in the lower left corner. 3. In the pop-up window, view the book catalog

Example of reading and writing CSV files in Java using OpenCSV Introduction: CSV (Comma-SeparatedValues) is a common text file format, usually used to store tabular data. In Java, OpenCSV is a popular open source library that can be used to handle reading and writing of CSV files. This article will introduce how to use OpenCSV to read and write CSV files, including reading and parsing CSV files, and CSV files
