Node.js file System


This module's main aim is to make file uploads even easier in Node.JS. In it's simplest form, when instantiated.

File I/O is provided by simple wrappers around standard POSIX functions. To use this module do require('fs'). All the methods have asynchronous and synchronous forms.

The asynchronous form always takes a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined.

When using the synchronous form any exceptions are immediately thrown. You can use try/catch to handle exceptions or allow them to bubble up.

Here is an example of the asynchronous version:

With the asynchronous methods there is no guaranteed ordering
const fs = require('fs');
fs.unlink('/app/demo', (err) => {
  if (err) throw err;
  console.log('successfully deleted /app/demo');
});

Here is an example of the synchronous version:

const fs = require('fs');
fs.unlinkSync('/app/demo');
console.log('successfully deleted /app/demo');

In busy processes, the programmer is strongly encouraged to use the asynchronous versions of these calls. The synchronous versions will block the entire process until they complete.

1. Read File:

Create dummy file with some text as hello.txt and will try to read that file using below code

var fs = require('fs');
fs.readFile('hello.txt', function (err, data) {
	if (!err) {
		console.log(data.toString());
		
	}
});

2. Write File:

Create dummy file with some text as hello.txt and will try to read that file using below code

fs = require('fs');
fs.writeFile('hello1.txt', 'Hello World!', function (err) {
  if (err) return console.log(err);
  console.log('Hello World > hello1.txt');
});

3. Delete File:

var fs = require('fs');
fs.unlink('polo1.txt', function (err) {
  if (err) throw err;
  console.log('successfully deleted polo1.txt');
});

4. Search File:

if you want to do a recursive list of files, then things get much more complicated very quickly. To avoid all of this scary complexity, this is one of the places where a Node.js user-land library can save the day. Node-findit, by SubStack, is a helper module to make searching for files easier. It has interfaces to let you work with callbacks, events, or just plain old synchronously (not a good idea most of the time)..

To install node-findit, simply use npm:

https://github.com/substack/node-findit
npm install findit

//This sets up the file finder
var finder = require('findit').find(__dirname);

//This listens for directories found
finder.on('directory', function (dir) {
  console.log('Directory: ' + dir + '/');
});

//This listens for files found
finder.on('file', function (file) {
  console.log('File: ' + file);
});

5. Append File:

Asynchronously append data to a file

var fs = require('fs');
fs.appendFile('hello1.txt', 'data to append', function(err) {
  if (err) throw err;
  console.log('The data appended');
});

fs.readFile('hello1.txt', function (err, data) {
	if (!err) {
		console.log("Latest Data::"+data.toString());
		
	}
});
Result for append file
The data appended
Latest Data::Hello World!data to appenddata to appenddata to append

6. Rename File name:

var fs = require('fs');
fs.rename('hello1.txt', 'polo1.txt', function (err) {
  if (err) throw err;
  console.log('renamed complete');
});