Day 2 File System
30 Days of Node
Introduction
Node.js gives the functionality of File I/O by providing wrappers around the standard POSIX functions. In Node.js, File I/O methods can be performed in both synchronous as well as asynchronous form depending upon the user requirements. In order to use this functionalities we need to require the fs module as shown below :
const fs = require('fs');
Reading a File in Node.js
There are two ways for Reading a file in node.js :
- Reading the file Asynchronously :
// Reading a File Asynchronously using node.js
const fs = require('fs');
fs.readFile('message.txt', (err, data) => {
if (err)
throw err;
console.log('Content : ' + data);
});
-
Reading the file Synchronously
const fs = require('fs'); // Name of the file to be read const filename = 'content.txt'; const content = fs.readFileSync(filename); console.log('Content: ' + content);
Write a File in Node.js
There are two ways for writing a file in Node.js
-
Writing the file Asynchronously
// Writing a File Asynchronously using node.js const fs = require('fs'); const content = "this is the content in the file"; fs.writeFile('message.txt', content, (err) => { if (err) throw err; console.log('It\'s saved!'); });
-
Writing the file Synchronously
const fs = require('fs'); const content = "We are writing this file synchronously using node.js"; fs.writeFileSync('content.txt', content); console.log('File Written Successfully');
Append a File using Node.js
There are two ways for Appending a file using node.js
-
Appending the file Asynchronously:
const fs = require('fs'); const new_data = "This data will be appended at the end of the file."; fs.appendFile('input.txt', new_data, (err) => { if(err) throw err; console.log('The new_content was appended successfully'); });
-
Appending the file Synchronously
// file append operation is node.js const fs = require('fs'); const content = "We are Appending this file synchronously using node.js" fs.appendFileSync('input.txt', content); console.log('File Appended Successful');
Rename a File in Node.js
There are two ways for Renaming a file in node.js:
-
Renaming the file Asynchronously:
const fs = require('fs'); // you have to pass the Relative path of the file from the current working directory. fs.rename('data.txt', 'new_data.txt', (err) => { if (err) throw err; console.log('File renamed successfully!'); }); // To check it's Asynchronous nature! console.log('This method is Asynchronous');
-
Renaming the file Synchronously:
const fs = require('fs'); // you have to pass the Relative path of the file from the Current working directory fs.renameSync('data.txt', 'newData.txt'); console.log('File renamed successfully!'); // To check it's Synchronous nature console.log('This method is Synchronous');
Delete a File in Node.js
There are two ways for deleting a file in Node.js
-
Deleting the file Asynchronously:
const fs = require('fs'); const filename = 'content.txt'; fs.unlink(filename, (err) => { if (err) throw err; console.log('File deleted successfully'); });
-
Deleting the file Synchronously:
const fs = require('fs'); const filename = 'data.txt'; fs.unlinkSync(filename); console.log('File Deleted Successfully');
Summary
In this part of node.js tutorial series we learned about file system in node.js which includes :
- Introduction to file system
- Read file operation using nodejs
- fs.readFile() : Read file in asynchronous way.
- fs.readFileSync() : Read file in synchronous way.
- Write file operation using nodejs
- fs.writeFile() : Write file in asynchronous way.
- fs.writeFileSync() : Write file in synchronous way.
- Append file operation using nodejs
- fs.appendFile() : Append file in asynchronous way.
- fs.appendFileSync() : Append file in synchronous way.
- Rename file operation using nodejs
- fs.rename() : Rename file name in asynchronous way.
- fs.renameSync() : Rename file name in synchronous way.
- Delete (unlink) file operation using nodejs
-
fs.unlink() : Delete file in asynchronous way.
in asynchronous way. - fs.renameSync() : Rename file name in synchronous way.
-
fs.unlink() : Delete file in asynchronous way.
- Delete (unlink) file operation using nodejs
- fs.unlink() : Delete file in asynchronous way.
- fs.unlinkSync() : Delete file in synchronous way.
上一篇: JDK1.7新特性(摘)
下一篇: 一张图看懂虚拟机中线程的共享区与私有区
推荐阅读
-
LOJ#6034. 「雅礼集训 2017 Day2」线段游戏【李超线段树】
-
「雅礼集训 2017 Day2」水箱 并查集+树形DP
-
【Day 2】GO语言新手入门:变量的类型和声明
-
Oracle数据备份过程中遇BUG_ORA-27054 NFS file system
-
Angular4集成ng2-file-upload的上传组件
-
JavaEE基础day02 1.定义Java中的变量 四类八种 2.变量定义和使用的注意事项 3.数据类型的转换、强制数据类型转换4.算数运算符、比较运算符、逻辑运算符、赋值运算符、三元运算符
-
Python学习笔记Day2
-
Day2----Python学习之路笔记(2)
-
google file system 用PHP获取Google AJAX Search API 数据的代码
-
google file system 用PHP获取Google AJAX Search API 数据的代码