1. File system module overview
A built-in FS module (file system module) is provided for operating files and directories. Through this module, you can perform file operations such as reading, writing, deleting, and renaming.
Before using the FS module, make sure your environment is installed correctly. You can run the following command in the terminal to confirm that it has been successfully installed:
node -v
If you see the version number, it means that the environment has been configured.
2. Read the file
Next, we'll see how to read the file. In, you can use()
Method to read the file content. This method supports asynchronous and synchronous readings.
Sample code: Asynchronously read files
First, we use an asynchronous method to read the file contents. Suppose we have a name calledThe content of the file is as follows:
Hello, ! Welcome to file operations.
Here is a code example for asynchronous reading of the file:
const fs = require('fs'); // Asynchronously read files('', 'utf8', (err, data) => { if (err) { ('An error occurred while reading the file:', err); return; } ('File content:', data); });
Sample code: Read files synchronously
If you want to use synchronous reading, you can use()
method. Please note that using the synchronization method can cause blockage when the file is large, so it is not recommended to use it in production environments.
const fs = require('fs'); try { const data = ('', 'utf8'); ('File content:', data); } catch (err) { ('An error occurred while reading the file:', err); }
3. Write to the file
In, you can use () and () to write files. () will overwrite the file contents, while () will add contents at the end of the file.
Sample code: Write to a file
The following is a code example for asynchronously writing to a file ():
const fs = require('fs'); const content = 'This is a piece of text written to the file. '; // Asynchronously write files('', content, (err) => { if (err) { ('An error occurred while writing to the file:', err); return; } ('File writing was successful!'); });
Sample code: Append to file
If you want to append content to an existing file, you can use()
method:
const fs = require('fs'); const additionalContent = 'This text will be appended to the end of the file. '; // Asynchronously append files('', additionalContent, (err) => { if (err) { ('An error occurred while appending the file:', err); return; } ('The content was successfully appended to the file!'); });
4. Error handling
Error handling is very important when performing file read and write operations. You can checkerr
Parameters to handle possible errors during read and write. Common errors include the file does not exist, insufficient permissions, etc.
Sample code: Handling file errors
Here is a complete example, including error handling:
const fs = require('fs'); // Read files asynchronously and handle errors('', 'utf8', (err, data) => { if (err) { ('An error occurred while reading the file:', ); return; } ('File content:', data); }); // Asynchronously write new files and handle errors('', 'This is the content of the new file。', (err) => { if (err) { ('An error occurred while writing to the file:', ); return; } ('File writing was successful!'); });
5. Precautions for synchronous and asynchronous
When you choose a synchronous or asynchronous method, please consider the following points:
- performance: The asynchronous method does not block event loops, and is suitable for I/O-intensive applications. Synchronization methods can cause performance degradation when reading large files.
-
Error handling: In asynchronous methods, error handling is the responsibility of the callback function, while in the synchronous methods, it can be used
try...catch
Perform capture.
6. Summary
In this article, we detail how to use the `fs module to read and write files. We show how to read and write files asynchronously and synchronously, and how to deal with common errors. After mastering these basic operations, you will be able to manage files more flexibly, thus providing more support for your front-end development work.
This is the end of this article about code examples for reading and writing files. For more related contents of reading and writing files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!