File system in Node.js
Basic article on file handling in Nodejs. File System module with read, write and delete operations.
File system concept
A file system is the structure of the operating system to organize, control and store data. File systems, in addition to control, add some additional features such as access layers, attributes, and conventions.
File systems in particular act in the OS as an interface to all other devices connected to the computer (USB sticks, external drives, etc). Currently, there are different types of file systems depending on the host OS, such as Windows, macOS, or Linux. These systems have some similarities, although in principle they are incompatible with each other.
The most common file systems to date are:
- Windows / FAT16, FAT32, exFAT y NTFS.
- MacOs / HFS+ y APFS.
- Linux / Ext4.
NOTE 🧐: To check the file system of your computer, consult the documentation of the respective OS. Windows example.
File concept
As related above, a file is basically a sequence of bytes that are stored on the device. They are called files because they are equivalent to physical files such as papers, notebooks, etc.
The particularity of the files is that they have a name and an associated extension commonly made up of 3 or 4 letters. The extension describes the content of the file, helping programs to be able to manipulate and display the content to the user as the case may be (JPG, PNG, TXT, etc).
NOTE 🧐: To get information about a certain file, check its properties. Windows example.
Module file system
Nodejs in particular contains a core module, especially for file manipulation called Filesystem. This module helps us with file support regardless of the operating system. The module works on the basis of callbacks, streams, and events like most modules in Nodejs.
Here is an example of a Nodejs program that reads the content of a text file located in the current directory.
const fs = require('fs');
fs.readFile('./hello.txt', { encoding: 'utf8'}, (err, data)=> {
if (err) throw err;
console.log(data);
});
This prints the following output to the console:
hello world
NOTE 🧐: The previous result, in particular, is a buffer, when we read we must indicate the encoding to interpret the content.
Basic file handling methods
The File system module contains the following basic methods for file manipulation:
- File reading: readFile y readFileSync.
- Writing files: writeFile y writeFileSync.
- File deletion: unlink y unlinkSync.
Depending on the needs, Nodejs provides us with these methods in an asynchronous mode by default and optionally synchronous (Sync).
File reading
Here is an example of a Nodejs program that reads the content of a text file asynchronously and synchronously.
readFile
const fs = require('fs');
fs.readFile('./words.txt', { encoding: "utf-8" }, (err, data) => {
if (err) throw err;
console.log(data);
});
readFileSync
const fs = require('fs');
try {
const data = fs.readFileSync('./words.txt', { encoding: "utf-8" });
console.log(data);
} catch (err) {
throw err;
}
These two ways print the same result to the console:
word1 word2
Writing files
Here is an example of a Nodejs program that creates a new file and writes the content asynchronously and synchronously.
writeFile
const fs = require('fs');
const text = "Hello world";
fs.writeFile('./file.txt', text, (err) => {
if (err) throw err;
});
writeFileSync
const fs = require('fs');
const text = "Hello world";
try {
fs.writeFileSync('./file.txt', text);
} catch (err) {
throw err;
}
These two ways create the file named file.txt in the current directory with the content of:
Hello world
File deletion
Here is an example of a Nodejs program that deletes a file in the current directory asynchronously and synchronously.
unlink
const fs = require('fs');
fs.unlink('./file.txt', (err) => {
if (err) throw err;
});
unlinkSync
const fs = require('fs');
try {
fs.unlinkSync('./file.txt');
} catch (err) {
throw err;
}
Both of these ways delete the file.txt file in the current directory.
Resources Nodejs File System
- File System API: Documentation, concepts, and examples.
Thanks for coming this far, if you find this useful don’t forget to clap 👏. Subscribe to receive more content 🔔.
If you need additional help, please contact me 🤠.
Thank you very much for reading, I appreciate your time.