Node.js file system module

chapter


fs module

Node.js file system (fs) module allows you to access the file system.

Using the require()method, import the file system module:

var fs = require('fs');

File system module commonly used functions:

  • Reading file
  • Create a file
  • Update file
  • Delete Files
  • Rename the file

Reading file

fs.readFile() The method used to read files.

Consider the following HTML files in the current directory:

demofile1.html

<html>
<body>
<h1>My Header</h1>
<p>My paragraph.</p>
</body>
</html>

Create a js file, reads the HTML file, the file is returned to the client-side content:

Examples

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('demofile1.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(8080);

Save the above code as "demo_readfile.js" file, use the Node launch the file:

Start demo_readfile.js:

C:\Users\Your Name>node demo_readfile.js

Browser to access the Web site: HTTP: // localhost: 8080

Create a file

Method creates a new file, are the following:

  • fs.appendFile()
  • fs.open()
  • fs.writeFile()

fs.appendFile()The method specified content appended to the file. If the file does not exist, create the file:

Examples

Using the appendFile()method creates a new file:

var fs = require('fs');

fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

fs.open()Methods "flag" as the second parameter flag "w" denotes "write", to open the specified file for writing. If the file does not exist, create an empty file:

Examples

Using the open()method to create a new, empty file:

var fs = require('fs');

fs.open('mynewfile2.txt', 'w', function (err, file) {
  if (err) throw err;
  console.log('Saved!');
});

fs.writeFile()When the method of writing a file, if the file exists, will cover the original content; if the file does not exist, create a new file, write to:

Examples

Using the writeFile()method creates a new file:

var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

Update file

Methods of update files, are the following:

  • fs.appendFile()
  • fs.writeFile()

fs.appendFile()The method append the contents of the file:

Examples

At the end of the additional "mynewfile1.txt" file "This is my text."

var fs = require('fs');

fs.appendFile('mynewfile1.txt', ' This is my text.', function (err) {
  if (err) throw err;
  console.log('Updated!');
});

fs.writeFile()Replace the original contents of the file method:

Examples

Replace the file "mynewfile3.txt" content:

var fs = require('fs');

fs.writeFile('mynewfile3.txt', 'This is my text', function (err) {
  if (err) throw err;
  console.log('Replaced!');
});

Delete Files

To delete a file, use the fs.unlink()method.

Use fs.unlink()delete your files:

Examples

Delete "mynewfile2.txt":

var fs = require('fs');

fs.unlink('mynewfile2.txt', function (err) {
  if (err) throw err;
  console.log('File deleted!');
});

Rename the file

To rename a file, using the fs.rename()method.

fs.rename()Rename the file specified:

Examples

Rename "mynewfile1.txt" file name "myrenamedfile.txt":

var fs = require('fs');

fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
  if (err) throw err;
  console.log('File Renamed!');
});

upload files

Subsequent chapters introduce.

Guess you like

Origin www.cnblogs.com/jinbuqi/p/11546923.html