{"id":73279,"date":"2020-01-15T16:18:33","date_gmt":"2020-01-15T10:48:33","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=73279"},"modified":"2024-04-12T14:27:13","modified_gmt":"2024-04-12T08:57:13","slug":"nodejs-writing-and-updating-files","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/","title":{"rendered":"NodeJS Writing and Updating Files"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/mean-stack-tutorials\/\" target=\"_blank\" rel=\"noreferrer noopener\">Go back to Tutorial<\/a><\/p>\n\n\n<p>Use fs.writeFile() method to write data to a file. If file already exists then it overwrites the existing content otherwise it creates a new file and writes data into it.<\/p>\n<p>Syntax: fs.writeFile(filename, data[, options], callback)<\/p>\n<p>Parameter Description:<\/p>\n<ul>\n<li>filename: Full path and name of the file as a string.<\/li>\n<li>Data: The content to be written in a file.<\/li>\n<li>options: The options parameter can be an object or string which can include encoding, mode and flag. The default encoding is utf8 and default flag is &#8220;r&#8221;.<\/li>\n<li>callback: A function with two parameters err and fd. This will get called when write operation completes.<\/li>\n<\/ul>\n<p>The following example creates a new file called test.txt and writes &#8220;Hello World&#8221; into it asynchronously.<\/p>\n<p><u>Example: Creating &amp; Writing File<\/u><\/p>\n<p>var fs = require(&#8216;fs&#8217;);<\/p>\n<p>fs.writeFile(&#8216;test.txt&#8217;, &#8216;Hello World!&#8217;, function (err) {<\/p>\n<p>if (err)<\/p>\n<p>console.log(err);<\/p>\n<p>else<\/p>\n<p>console.log(&#8216;Write operation complete.&#8217;);<\/p>\n<p>});<\/p>\n<p>In the same way, use fs.appendFile() method to append the content to an existing file.<\/p>\n<p><u>Example: Append File Content<\/u><\/p>\n<p>var fs = require(&#8216;fs&#8217;);<\/p>\n<p>fs.appendFile(&#8216;test.txt&#8217;, &#8216;Hello World!&#8217;, function (err) {<\/p>\n<p>if (err)<\/p>\n<p>console.log(err);<\/p>\n<p>else<\/p>\n<p>console.log(&#8216;Append operation complete.&#8217;);<\/p>\n<p>});<\/p>\n<p>Open File<\/p>\n<p>Alternatively, you can open a file for reading or writing using fs.open() method.<\/p>\n<p>Syntax: fs.open(path, flags[, mode], callback)<\/p>\n<p>Parameter Description:<\/p>\n<ul>\n<li>path: Full path with name of the file as a string.<\/li>\n<li>Flag: The flag to perform operation<\/li>\n<li>Mode: The mode for read, write or readwrite. Defaults to 0666 readwrite.<\/li>\n<li>callback: A function with two parameters err and fd. This will get called when file open operation completes.<\/li>\n<\/ul>\n<p>Flags &#8211; The following table lists all the flags which can be used in read\/write operation.<\/p>\n<table>\n<thead>\n<tr>\n<td width=\"52\"><strong>Flag <\/strong><\/td>\n<td width=\"586\"><strong>Description<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"52\">r<\/td>\n<td width=\"586\">Open file for reading. An exception occurs if the file does not exist.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">r+<\/td>\n<td width=\"586\">Open file for reading and writing. An exception occurs if the file does not exist.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">rs<\/td>\n<td width=\"586\">Open file for reading in synchronous mode.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">rs+<\/td>\n<td width=\"586\">Open file for reading and writing, telling the OS to open it synchronously. See notes for &#8216;rs&#8217; about using this with caution.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">w<\/td>\n<td width=\"586\">Open file for writing. The file is created (if it does not exist) or truncated (if it exists).<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">wx<\/td>\n<td width=\"586\">Like &#8216;w&#8217; but fails if path exists.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">w+<\/td>\n<td width=\"586\">Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">wx+<\/td>\n<td width=\"586\">Like &#8216;w+&#8217; but fails if path exists.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">a<\/td>\n<td width=\"586\">Open file for appending. The file is created if it does not exist.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">ax<\/td>\n<td width=\"586\">Like &#8216;a&#8217; but fails if path exists.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">a+<\/td>\n<td width=\"586\">Open file for reading and appending. The file is created if it does not exist.<\/td>\n<\/tr>\n<tr>\n<td width=\"52\">ax+<\/td>\n<td width=\"586\">Like &#8216;a+&#8217; but fails if path exists.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The following example opens an existing file and reads its content.<\/p>\n<p><u>Example:File open and read<\/u><\/p>\n<p>var fs = require(&#8216;fs&#8217;);<\/p>\n<p>fs.open(&#8216;TestFile.txt&#8217;, &#8216;r&#8217;, function (err, fd) {<\/p>\n<p>if (err) {<\/p>\n<p>return console.error(err);<\/p>\n<p>}<\/p>\n<p>var buffr = new Buffer(1024);<\/p>\n<p>fs.read(fd, buffr, 0, buffr.length, 0, function (err, bytes) {<\/p>\n<p>if (err) throw err;<\/p>\n<p>\/\/ Print only read bytes to avoid junk.<\/p>\n<p>if (bytes &gt; 0) {<\/p>\n<p>console.log(buffr.slice(0, bytes).toString());<\/p>\n<p>}<\/p>\n<p>\/\/ Close the opened file.<\/p>\n<p>fs.close(fd, function (err) {<\/p>\n<p>if (err) throw err;<\/p>\n<p>});<\/p>\n<p>});<\/p>\n<p>});<\/p>\n<p>Renaming File<\/p>\n<p><strong>syntax &#8211; Asynchronous<\/strong><\/p>\n<p>fs.rename(oldPath, newPath, callback)<\/p>\n<p>oldPath &lt;String&gt; | &lt;Buffer&gt;<\/p>\n<p>newPath &lt;String&gt; | &lt;Buffer&gt;<\/p>\n<p>callback &#8211; &lt;Function&gt;<\/p>\n<p><strong>syntax &#8211; Synchronous<\/strong><\/p>\n<p>fs.renameSync(oldPath, newPath)<\/p>\n<p>oldPath &lt;String&gt; | &lt;Buffer&gt;<\/p>\n<p>newPath &lt;String&gt; | &lt;Buffer&gt;<\/p>\n<p>file rename example (asynchronously)<\/p>\n<p>var fs = require(&#8216;fs&#8217;);<\/p>\n<p>fs.rename(&#8216;c:\\\\dog.jpg&#8217;, &#8216;c:\\\\dog-rename.jpg&#8217;, function(err) {<\/p>\n<p>if (err) {<\/p>\n<p>console.log(&#8216;ERROR: &#8216; + err);<\/p>\n<p>throw err;<\/p>\n<p>}<\/p>\n<p>console.log(&#8216;File renamed!!&#8217;);<\/p>\n<p>});<\/p>\n<p>output<\/p>\n<p>File renamed!!<\/p>\n<p>file rename example (asynchronously)<\/p>\n<p>var fs = require(&#8216;fs&#8217;);<\/p>\n<p>fs.renameSync(&#8216;c:\\\\dog.jpg&#8217;, &#8216;c:\\\\dog-rename.jpg&#8217;);<\/p>\n<p><strong>Delete File<\/strong><\/p>\n<p>Use fs.unlink() method to delete an existing file.<\/p>\n<p>Syntax: fs.unlink(path, callback);<\/p>\n<p>The following example deletes an existing file.<\/p>\n<p><u>Example:File Open and Read<\/u><\/p>\n<p>var fs = require(&#8216;fs&#8217;);<\/p>\n<p>fs.unlink(&#8216;test.txt&#8217;, function () {<\/p>\n<p>console.log(&#8216;write operation complete.&#8217;);<\/p>\n<p>});<\/p>\n<p><strong>Important method of fs module<\/strong><\/p>\n<table width=\"650\">\n<thead>\n<tr>\n<td width=\"315\"><strong>Method <\/strong><\/td>\n<td width=\"335\"><strong>Description<\/strong><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"315\">fs.readFile(fileName [,options], callback)<\/td>\n<td width=\"335\">Reads existing file.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.writeFile(filename, data[, options], callback)<\/td>\n<td width=\"335\">Writes to the file. If file exists then overwrite the content otherwise creates new file.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.open(path, flags[, mode], callback)<\/td>\n<td width=\"335\">Opens file for reading or writing.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.rename(oldPath, newPath, callback)<\/td>\n<td width=\"335\">Renames an existing file.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.chown(path, uid, gid, callback)<\/td>\n<td width=\"335\">Asynchronous chown.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.stat(path, callback)<\/td>\n<td width=\"335\">Returns fs.stat object which includes important file statistics.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.link(srcpath, dstpath, callback)<\/td>\n<td width=\"335\">Links file asynchronously.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.symlink(destination, path[, type], callback)<\/td>\n<td width=\"335\">Symlink asynchronously.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.rmdir(path, callback)<\/td>\n<td width=\"335\">Renames an existing directory.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.mkdir(path[, mode], callback)<\/td>\n<td width=\"335\">Creates a new directory.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.readdir(path, callback)<\/td>\n<td width=\"335\">Reads the content of the specified directory.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.utimes(path, atime, mtime, callback)<\/td>\n<td width=\"335\">Changes the timestamp of the file.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.exists(path, callback)<\/td>\n<td width=\"335\">Determines whether the specified file exists or not.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.access(path[, mode], callback)<\/td>\n<td width=\"335\">Tests a user&#8217;s permissions for the specified file.<\/td>\n<\/tr>\n<tr>\n<td width=\"315\">fs.appendFile(file, data[, options], callback)<\/td>\n<td width=\"335\">Appends new content to the existing file.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/mean-stack-tutorials\/\" target=\"_blank\" rel=\"noreferrer noopener\">Go back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go back to Tutorial Use fs.writeFile() method to write data to a file. If file already exists then it overwrites the existing content otherwise it creates a new file and writes data into it. Syntax: fs.writeFile(filename, data[, options], callback) Parameter Description: filename: Full path and name of the file as a string. Data: The content&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8514],"tags":[8526],"class_list":["post-73279","page","type-page","status-publish","hentry","category-mean-stack","tag-nodejs-writing-and-updating-files"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>NodeJS Writing and Updating Files - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NodeJS Writing and Updating Files - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Go back to Tutorial Use fs.writeFile() method to write data to a file. If file already exists then it overwrites the existing content otherwise it creates a new file and writes data into it. Syntax: fs.writeFile(filename, data[, options], callback) Parameter Description: filename: Full path and name of the file as a string. Data: The content...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:57:13+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/\",\"name\":\"NodeJS Writing and Updating Files - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-15T10:48:33+00:00\",\"dateModified\":\"2024-04-12T08:57:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NodeJS Writing and Updating Files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NodeJS Writing and Updating Files - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/","og_locale":"en_US","og_type":"article","og_title":"NodeJS Writing and Updating Files - Tutorial","og_description":"Go back to Tutorial Use fs.writeFile() method to write data to a file. If file already exists then it overwrites the existing content otherwise it creates a new file and writes data into it. Syntax: fs.writeFile(filename, data[, options], callback) Parameter Description: filename: Full path and name of the file as a string. Data: The content...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:57:13+00:00","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/","name":"NodeJS Writing and Updating Files - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-15T10:48:33+00:00","dateModified":"2024-04-12T08:57:13+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/nodejs-writing-and-updating-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"NodeJS Writing and Updating Files"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=73279"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73279\/revisions"}],"predecessor-version":[{"id":87756,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/73279\/revisions\/87756"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=73279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=73279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=73279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}