Tutorial on Node File System - PowerPoint PPT Presentation

About This Presentation
Title:

Tutorial on Node File System

Description:

The Node.js file system module allows you to work with the file system on your computer. 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. – PowerPoint PPT presentation

Number of Views:382

less

Transcript and Presenter's Notes

Title: Tutorial on Node File System


1
iFour Consultancy
https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
2
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.
  • The Node.js file system module allows you to work
    with the file system on your computer.
  • To include the File System module, use the
    require() method
  • var fs require('fs')

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
3
Use of File System
  • Common use for the File System module
  • Read files
  • Write files
  • Append files
  • Delete files
  • Rename files

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development

4
Synchronous vs Asynchronous
  • Every method in the fs module has synchronous as
    well as asynchronous forms. Asynchronous methods
    take the last parameter as the completion
    function callback and the first parameter of the
    callback function as error. It is better to use
    an asynchronous method instead of a synchronous
    method, as the former never blocks a program
    during its execution, whereas the second one does.

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
5
Node.js Reading File
  • Every method in fs module has synchronous and
    asynchronous forms.
  • Asynchronous methods take a last parameter as
    completion function callback. Asynchronous method
    is preferred over synchronous method because it
    never blocks the program execution where as the
    synchronous method blocks.
  • Example
  • Create File input.txt
  • We are reading this file using node.js

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
6
Node.js Reading File
  • Example Create File main.js
  • For Asynchronous read
  • var fs require("fs")
  • // Asynchronous read
  • fs.readFile('input.txt', function (err, data)
  • if (err)
  • return console.error(err)
  • console.log("Asynchronous read "
    data.toString())

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
7
Node.js Reading File
  • Example Create File main.js
  • For Synchronous read
  • var data fs.readFileSync('input.html')
  • console.log("Synchronous read "
    data.toString())
  • console.log("Program Ended")
  • Open command prompt and run the main.js
  • gt node main.js

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
8
Node.js Writing a file
  • Node.js fs.writeFile() function writes data to a
    file asynchronously with replacing the file in
    case of already exists. This function can write
    data from string or a buffer.
  • The encoding option is ignored if data is a
    buffer. It defaults encoding is utf8, Default
    file mode is 0666 and default flag is used w
    means write mode.
  • 1. path is the filename with path.
  • 2. data is the String or buffer to write
  • 3. options can be an object which is like
    encoding, mode, flag.
  • 4. callback function takes single parameter err
    and used to return errors.
  • Syntax
  • fs.writeFile(filename, data, options,
    callback)

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
9
Node.js Writing a file
  • There are two ways for writing a file in nodejs
  • Example Create File main.js
  • For Asynchronous Write
  • var fs require('fs')
  • var content "this is the content in the file"
  • fs.writeFile('input.txt', content , (err) gt
  • if (err)
  • throw err
  • console.log('It\'s saved!')
  • )

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
10
Node.js Writing a file
  • Example Create File main.js
  • For Synchronous write
  • var fs require('fs')
  • var content "We are writing this file
    synchronously using node.js"
  • fs.writeFileSync('input.txt', content)
  • console.log("File Written Successfully")
  • Open command prompt and run the main.js
  • gt node main.js

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
11
Append a File using Nodejs
  • To append data to file in Node.js, use Node FS
    appendFile() function for asynchronous file
    operation or Node FS appendFileSync() function
    for synchronous file operation.
  • filepath is a String that specifies file path
  • data is what you append to the file
  • options to specify encoding/mode/flag
  • Syntax
  • fs.appendFile(filepath, data, options,
    callback_function)
  • fs.appendFileSync(filepath, data, options)

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
12
Append a File using Nodejs
  • There are two ways for Appending a file using
    nodejs
  • Example Create File main.js
  • For Asynchronous Append
  • var fs require('fs')
  • new_data "This data will be appended at the
    end of the file."
  • fs.appendFile('input.txt', new_data , (err) gt
  • if(err)
  • throw err
  • console.log('The new_content was appended
    successfully')
  • )

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
13
Append a File using Nodejs
  • Example Create File main.js
  • For Synchronous Append
  • var fs require('fs')
  • var content "We are Appending this file
    synchronously using node.js"
  • fs.appendFileSync('input.txt', content)
  • console.log("File Appended Successfully")
  • Open command prompt and run the main.js
  • gt node main.js

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
14
Rename a File in Nodejs
  • To rename file with Node FS, use
    fs.rename(new_file_name, old_file_name,
    callback_function) for asynchronous file rename
    operation and use fs.renameSync(new_file_name,
    old_file_name) for synchronous file rename
    operation.
  • new_file_path The new file path you would
    like to assign
  • old_file_path Path to the file whose name is
    to be changed
  • callback_function When file renaming operation
    is done, Callback Function is
    called with an error object.
  • Syntax
  • fs.rename(new_file_path, old_file_path,
    callback_function)
  • fs.renameSync(new_file_path, old_file_path)

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
15
Rename a File in Nodejs
  • There are two ways for Appending a file using
    nodejs
  • Example Create File main.js
  • For Asynchronous Rename
  • var fs require('fs')
  • fs.rename('input.txt', 'newinput.txt', (err) gt
  • if (err)
  • throw err
  • console.log('File renamed successfully')
  • )
  • console.log("This method is Asynchronous")

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
16
Rename a File in Nodejs
  • Example Create File main.js
  • For Synchronous Rename
  • var fs require('fs')
  • fs.renameSync('input.txt', 'newinput.txt')
  • console.log('File renamed successfully')
  • console.log("This method is Synchronous")
  • Open command prompt and run the main.js
  • gt node main.js

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
17
Delete a File in Nodejs
  • To delete a file in Node.js, Node FS unlink(path,
    callback) can be used for asynchronous file
    operation and unlinkSync(path) can be used for
    synchronous file operation.
  • Syntax
  • fs.unlink(filePath, callbackFunction)
  • fs.unlinkSync(filePath)

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
18
Delete a File in Nodejs
  • There are two ways for Appending a file using
    nodejs
  • Example Create File main.js
  • For Asynchronous Delete var fs
    require('fs')
  • var filename 'input.txt'
  • fs.unlink(filename, (err) gt
  • if (err)
  • throw err
  • console.log('File deleted successfully')
  • )

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
19
Delete a File in Nodejs
  • Example Create File main.js
  • For Synchronous Delete
  • var fs require('fs')
  • var filename 'input.txt'
  • fs.unlinkSync(filename)
  • console.log('File Deleted Successfully')
  • Open command prompt and run the main.js
  • gt node main.js

https//www.ifourtechnolab.com/nodejs-blockchain-s
oftware-development
Write a Comment
User Comments (0)
About PowerShow.com