- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js Read and Write File
The fs
module in Node.js allows you to read from and write to files. It provides both synchronous and asynchronous methods for these operations, making it flexible for different application requirements.
Key Features of File Read/Write
- Asynchronous and Synchronous Operations: Both methods allow handling files efficiently, either asynchronously (non-blocking) or synchronously (blocking).
- Error Handling: Proper error handling is crucial when working with files to ensure the smooth operation of applications.
- Text and Binary Data: You can read and write both text and binary data from/to files.
Reading Files
1. fs.readFile()
The fs.readFile()
method is used to read the contents of a file asynchronously.
Output:
- The file is read asynchronously, and the callback function is invoked once the data is available.
2. fs.readFileSync()
The fs.readFileSync()
method is a synchronous version of fs.readFile()
. It blocks execution until the file is fully read.
Output:
- The method will block further code execution until the entire file is read.
Writing Files
1. fs.writeFile()
The fs.writeFile()
method is used to write data to a file asynchronously.
Output:
- This method writes the data to the file asynchronously and invokes the callback when the operation is complete.
2. fs.writeFileSync()
The fs.writeFileSync()
method is a synchronous version of fs.writeFile()
. It blocks execution until the file is fully written.
Output:
- The method writes data synchronously, blocking the rest of the code until the operation completes.
3. fs.appendFile()
The fs.appendFile()
method is used to append data to a file. If the file doesn't exist, it will be created.
Output:
- The method appends text to the end of the file asynchronously.
Summary
Node.js provides efficient methods for reading from and writing to files using the fs
module. You can use asynchronous methods like fs.readFile()
and fs.writeFile()
to avoid blocking the event loop, or synchronous methods like fs.readFileSync()
and fs.writeFileSync()
for simpler workflows. Appending data to files is also possible with the fs.appendFile()
method. These operations are crucial for building applications that require file manipulation and management.