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"
<h1>Node.js Frameworks</h1>

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


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"


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');
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);


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. 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'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
//Deleting files in node.js
var fs=require('fs');
if(error) throw error;
console.log('File deleted');



Renaming Files using fs.rename()

//Renaming files in node.js
var fs=require('fs');
if(error) throw error;
console.log('File Renamed');


Automatically reloading Node.js application when you make changes.



android android Apps android emulators 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
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
Java Codes
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