Nodejs file and file actions (read-write files delete rename)

Source: Internet
Author: User
Tags lstat

Read and write files
Nodejs in the operation is relatively simple and much! Let's take a look at some examples.


"Write Text File"
Wfile.js
------------------------------

The code is as follows Copy Code

var fs = require ("FS");

var data = ' Hello Rainforest blog ';
Fs.writefile (' C:a.txt ', data, ' ASCII ', function (err) {
if (err) {
Console.log (' Write file failed ');
}else{
Console.log (' Save success, hurry to see garbled ');
}
})


------------------------------
[Note: By default, the data is encoded as utf8;mode=438; Flag=w]

"Read text file"
Rfile.js
------------------------------

The code is as follows Copy Code
var fs = require ("FS");

var data = ' Hello Rainforest blog ';
Fs.readfile (' c:a.txt ', ' ASCII ', function (err, data) {
if (err) {
Console.log (' Write file failed ');
}else{
Console.log (data);
}
})

------------------------------
[Note: If there is no special encoding, then the binary buffered data is returned.] ]
[Note: Binary buffered data printing results: <buffer 6c 6c 6f E8 A2>]

File directory Operations


Nodejs file operation (FS)


When manipulating files, we need to require (load) the file System package to obtain the document operation function.

The code is as follows Copy Code

var fs = require ("FS");


And this fs how to use it, this time we need to query Nodejs official API


Http://nodejs.org/api/fs.html

Basic file operations include: New, rename, delete, and so on, to see a few examples


"New Folder"

The code is as follows Copy Code
Newfolder.js
-----------------------------
Load File System module
var fs = require ("FS");

Create a folder named A on the C disk
Fs.mkdir ("C:a", function (Err) {
if (!err) {
Console.log ("Operation successful!") ");
}else{
Console.log (The operation failed!) ");
}
});

-----------------------------
[Note: If the folder exists err, there will be an error message.] ]

"Delete Folder"

The code is as follows Copy Code
Removefolder.js
-----------------------------
var fs = require ("FS");

Delete Folder A in C disk
Fs.rmdir ("C:a", function (Err) {
if (err) {
Console.log (delete failed!) ");
}else{
Console.log ("Delete succeeded!") ");
}
});

-----------------------------
[Note: If the delete folder does not exist, err will have an error message.] ]


"Rename Folder"
Rename.js
-----------------------------

The code is as follows Copy Code

var fs = require ("FS");

Rename Folder A to B
Fs.rename ("C:a", "c:b", function (Err) {
if (err) {
Console.log (rename failed!) ");
}else{
Console.log ("Rename succeeded!") ");
}
});

-----------------------------
[Note: The folder does not exist, so err has an error message.] ]


"Determine if a file/folder exists"

The code is as follows Copy Code

Exsits.js
-----------------------------

var fs = require ("FS");

Determine if a folder exists
Fs.exists ("C:a", function (exists) {
if (exists) {
Console.log ("A folder exists");
}else{
Console.log ("folder A does not exist")
}
});

-----------------------------
[Note: Then call the ' callback argument with either True or false
(This callback function parameter value is TRUE or false)]

"Judge file Type" [Fs.stat (), Fs.lstat () and Fs.fstat ()]
Stat.js
-----------------------------

The code is as follows Copy Code
var fs = require ("FS");

Get the type of folder a
Fs.stat ("C:a", Function (err, stat) {
if (err) {
Console.log ("file does not exist!") ");
}else{
Console.log ("Whether file:" +stat.isfile ());
Console.log ("Whether folder:" +stat.isdirectory ());
}
});

-----------------------------
[
Other State functions:

Stats.isfile ()
Stats.isdirectory ()
Stats.isblockdevice ()
Stats.ischaracterdevice ()
Stats.issymboliclink () (only valid for Fs.lstat ())
Stats.isfifo ()
Stats.issocket ()]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.