Publicidad

FS_module_functions.pptx

Education Employee en MIT
30 de Jan de 2023
Publicidad

Más contenido relacionado

Publicidad

FS_module_functions.pptx

  1. CH 6: FILE SYSTEM Prepared By: Bareen Shaikh
  2. Common use for the File System module  Read files: fs.readFile() fs.readFileSync()  Create files: fs.open() fs.writeFile()  Update files: fs.appendFile() fs.writeFile()  Delete files: fs.unlink()  Rename files: fs.rename()
  3. Creating a file: fs.open()  fs.open() method does several operations on a file.  Syntax: fs.open( filename, flags, mode, callback )  Parameter: This method accept four parameters as below:  filename: It holds the name of the file to read or the entire path if stored at other location.  flag: The operation in which file has to be opened.  mode: Sets the mode of file i.e. r-read, w-write, r+ - readwrite. It sets to default as readwrite.  callback: It is a callback function that is called after reading a file. It takes two parameters:  err: If any error occurs.
  4. Flags of fs.open()  All the types of flags are described below:  r: To open file to read and throws exception if file doesn’t exists.  r+: Open file to read and write. Throws exception if file doesn’t exists  rs+: Open file in synchronous mode to read and write.  w: Open file for writing. File is created if it doesn’t exists.  wx: It is same as ‘w’ but fails if path exists.  w+: Open file to read and write. File is created if it doesn’t exists.  wx+: It is same as ‘w+’ but fails if path exists.  a: Open file to append. File is created if it doesn’t exists.  ax: It is same as ‘a’ but fails if path exists.  a+: Open file for reading and appending. File is created if it doesn’t exists.
  5. Example of : fs.open() var fs = require('fs'); // Open file a.txt in read mode fs.open(‘a.txt', 'r', function (err, data) { console.log(data); console.log('Saved!'); });
  6. Reading a file: fs.readFile()  The fs.readFile() method is an inbuilt method which is used to read the file. This method read the entire file into buffer.  Using fs.readFile() method, file can be read in a non- blocking asynchronous way.  Syntax: fs.readFile( filename, encoding, callback_function ) Parameters: The method accept three parameters as below:  filename: It holds the name of the file to read or the entire path if stored at other location.  encoding: It holds the encoding of file. Its default value is ‘utf8’.  callback function: It is a callback function that is called after reading of file. It takes two parameters:
  7. Example var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { fs.readFile(‘app.js', function(err, data) { if(err) console.log(err); else { res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); res.end(); } }); }).listen(8080);
  8. Reading File: fs.readFileSync()  The fs.readFileSync() method is an inbuilt application programming interface of fs module which is used to read the file and return its content.  fs.readFileSync() method, read files in a synchronous way, i.e. we are telling node.js to block other parallel process and do the current file reading process.  When the fs.readFileSync() method is called  The original node program stops executing  Executing node waits for the fs.readFileSync() function to get executed  After completion of the fs.readFileSync() method the remaining node program is executed.
  9. Syntax of fs.readFileSync()  Syntax: fs.readFileSync( path, options )  Parameters:  path: It takes the relative path of the text file. The path can be of URL type.  options: It is an optional parameter which contains the encoding and flag, the encoding contains data specification. It’s default value is null which returns raw buffer and the flag contains indication of operations in the file. It’s default value is ‘r’.  Example: const fs = require('fs'); const data = fs.readFileSync(‘app.js', {encoding:'utf8', flag:'r'});
  10. Example of fs.readFile() & fs.readFileSync() const fs = require('fs'); // Calling the fs.readFile() method for reading file fs.readFile(‘a.txt', {encoding:'utf8', flag:'r'}, function(err, data) { if(err) console.log(err); else console.log(data); }); // Calling the fs.readFileSync() method for reading file ‘b.txt' const data = fs.readFileSync(‘b.txt', {encoding:'utf8', flag:'r'});
  11. Writing to a File: fs.writeFile()  The fs.writeFile() method is used to asynchronously write the specified data to a file.  The file would be replaced if it ex  Syntax: fs.writeFile( file, data, options, callback )  Parameters: This method accept four parameters as mentioned above and described below:  file: It is a string denotes the path of the file where it has to be written.  data: It is a string, Buffer, TypedArray or DataView that will be written to the file.  options: It is an string or object that can be used to specify optional parameters that will affect the output. It has three optional parameter: encoding: It is a string value that specifies the encoding of the file. The default value is ‘utf8’. mode: It is an integer value that specifies the file mode. The default value is 0o666. flag: It is a string value that specifies the flag used while writing to the file. The default value is ‘w’.  callback: It is the function that would be called when the method is executed.
  12. Example of fs.writeFile() const fs = require('fs'); let data = "This is a file containing a a data n1111n 2222n 3333"; fs.writeFile(“a.txt", data, (err) => { if (err) console.log(err); else { console.log("File written successfullyn"); console.log("The written has the following contents:"); console.log(fs.readFileSync(“a.txt", "utf8")); } });
  13. Writing to a File: fs.writeFileSync()  The fs.writeFileSync() is a synchronous method.  The fs.writeFileSync() creates a new file if the specified file does not exist.  Syntax: fs.writeFileSync( file, data, options )  Parameters: This method accept three parameters as follows: file: It is a string, that denotes the path of the file where it has to be written. data: It is a string, Buffer, TypedArray or DataView that will be written to the file. options: It is an string or object that can be used to specify optional parameters that will affect the output. It has three optional parameter:  encoding: It is a string which specifies the encoding of the file. The default value is ‘utf8’.  mode: It is an integer which specifies the file mode. The default value is 0o666.  flag: It is a string which specifies the flag used while writing to the file.
  14. Example of fs.writeFileSync() const fs = require('fs'); let data = "This is a file containing a collection" + " of programming languages.n" + "1. Cn2. C++n3. Python"; fs.writeFileSync(“programming.txt", data); console.log("File written successfullyn"); console.log("The written has the following contents:"); console.log(fs.readFileSync("programming.txt", "utf8"));
  15. Appending to file fs.appendFile()  fs.appendFile() method is used to asynchronously append the given data to a file.  A new file is created if it does not exist.  Syntax fs.appendFile( path, data[, options], callback)  Parameters: This method accepts four parameters as mentioned above and described below: path: It is a String, Buffer, URL or number that denotes the source filename or file descriptor that will be appended to. data: It is a String or Buffer that denotes the data that has to be appended. options: It is an string or an object that can be used to specify optional parameters that will affect the output. It has three optional parameters:  encoding: It is a string which specifies the encoding of the file. The default value is ‘utf8’.  mode: It is an integer which specifies the file mode. The default value is ‘0o666’.  flag: It is a string which specifies the flag used while appending to the file. The default value is ‘a’. callback: It is a function that would be called when the method is executed.
  16. Appending to file fs.appendFile() const fs = require('fs'); // Get the file contents before the append operation console.log("nFile Contents of file before append:", var data=fs.readFileSync(“programming.txt”,{}) fs.readFileSync("example_file.txt", "utf8")); fs.appendFile("example_file.txt", data, (err) => { if (err) { console.log(err); } else { // Get the file contents after the append operation console.log("nFile Contents of file after append:“), console.log(fs.readFileSync("example_file.txt", "utf8")); } });
  17. Var fs=require(‘fs’)  Open  fs.open()  Write  fs.writeFileSync()  fs.writeFile()  Reading  fs.readFileSync()  fs.readFile()  Append  fs.appenFile() //asyn
Publicidad