Reading,Updating,Deleting and Renaming Files Using Node.js

Reading,Updating,Deleting and Renaming Using Node.js

In this tutorial, we are going to learn how to read, update or edit, delete and rename files in Node.js.Previously we learnt how to  Use Node.js Http module and creating modules.

Let get Started.

Reading Html File

To read files using node.js we use readFile() function
Save below html code as "demo.html"
<html>
<body>
<h1>Node.js Frameworks</h1>
<ol>
<li>
React.js
</li>
<li>Ember.js</li>
<li>Express.js</li>
</ol>
</body>
</html>

Now let write node.js code to read above html file and display its content in a browser.
Save the code as "readfile.js"


var http=require('http');//import http module
var fs=require('fs')//import files module
var server=http.createServer(function(req,res)//create a server
{
fs=fs.readFile('demo.html',function(error,data)
{
res.writeHead(200,{'Content-type':'text/html'});
res.write(data);
res.end();

});

});
server.listen(8080);//make the server to listen to port 8080


Running the code.

Open Node.js command prompt.

Open the folder containing the two files.My folder is "nprojects"

Run the code by entering  "node creatfile.js"

Output:



Reading a txt file

We can also read a .txt file and display content in the console.
Example .

Create a file and name it "file.txt"

Add content to the file and then read it using code.



//Reading file in a Synchronous way
var fs=require('fs');
fs.readFile('file.txt',function(error,data)
{
if(error) throw error;
console.log('Synchronous read:= ' +data.toString());

})
//Reading file Asynchronous way
var content=fs.readFileSync('file.txt','utf8');
console.log('Asybchronous read:= ' +content);


Output.


Updating Files

To update files in Node.js, we can use the following functions.
 1.fs.appendFile()-opens a specified file and adds its contents.If the file does not exists its created.
 2.fs.open()-this file takes a certain flag.The flag determines if the file is being opened for writing, reading etc.To write to the file we use "w" flag.If the file does not exist its created and if it exists its opened for writing
3.fs.writeFile()-this function creates a file if it does not exists and if it exists content is added to it.

//Function to create file and add content appendFile function
var fs=require('fs');
fs.appendFile('mynewfile.txt',"My fist content",function(error)
{
if(error) throw error;
console.log("File created and saved");
})
//Function to Open file and write to it
fs.open('mynewfile2.txt','w',function(error,file)
{
fs.write(file,'New content added',function(error){
if(error) throw error;
console.log("New content addded");
});})
//WriteFile function.
fs.writeFile('mynewfile3.txt','Writing file number 3',function(error)
{
if(error) throw error;
console.log('File number 3 written')
})




Deleting Files


To delete files in Node.js we use fs.unlink() function.If the file specified file does not exist, an error is thrown
Example
//Deleting files in node.js
var fs=require('fs');
fs.unlink('mynewfile.txt',function(error)
{
if(error) throw error;
console.log('File deleted');

})

Output


Renaming Files using fs.rename()

//Renaming files in node.js
var fs=require('fs');
fs.rename('mynewfile.txt',function(error)
{
if(error) throw error;
console.log('File Renamed');

})

Automatically reloading Node.js application when you make changes.






















COMMENTS

Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Reading,Updating,Deleting and Renaming Files Using Node.js
Reading,Updating,Deleting and Renaming Files Using Node.js
Reading,Updating,Deleting and Renaming Using Node.js
https://4.bp.blogspot.com/-RDy21lxcXp8/WnzK8Z7O6JI/AAAAAAAAKJQ/xN-VspV41D8zYeYrbuuajn18hawki_XRQCLcBGAs/s320/readfiles-using-node-module.PNG
https://4.bp.blogspot.com/-RDy21lxcXp8/WnzK8Z7O6JI/AAAAAAAAKJQ/xN-VspV41D8zYeYrbuuajn18hawki_XRQCLcBGAs/s72-c/readfiles-using-node-module.PNG
Java Codes
https://www.download-all.net/2018/02/readingupdatingdeleting-and-renaming.html
https://www.download-all.net/
https://www.download-all.net/
https://www.download-all.net/2018/02/readingupdatingdeleting-and-renaming.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy