Nodejs file and file operations (read/write file deletion and rename)

Source: Internet
Author: User
Tags lstat

I learned about node. js most. I learned about the node. js file system today. Here I am looking for some file directory operation instances for node. js file systems. I will share them with you below.

Read/write files
Operations in nodejs are much easier! Let's take a look at several examples.


[Write a text file]
// Wfile. js
------------------------------

The Code is as follows: Copy code

Var fs = require ("fs ");

Var data = 'Hello dashboard ';
Fs. writeFile ('C: a.txt ', data, 'ascii', function (err ){
If (err ){
Console. log ('file writing failed ');
} Else {
Console. log ('Save successfully, hurry and check for garbled id ');
}
})


------------------------------
[Note: by default, the data encoding is utf8; mode = 438; flag = w]

 

[Read text files]
// Rfile. js
------------------------------

The Code is as follows: Copy code
Var fs = require ("fs ");

Var data = 'Hello dashboard ';
Fs. readFile ('C: a.txt ', 'ascii', function (err, data ){
If (err ){
Console. log ('file writing failed ');
} Else {
Console. log (data );
}
})

------------------------------
[NOTE: If no special encoding is available, binary data is returned as a buffer.]
[Note: Binary Buffer data printing result: <Buffer 68 65 6c 6c 6f 20 e8 97 5a a2>]

File directory operations


Nodejs file operations (fs)


When operating files, we need to require (load) the File System package to obtain the File operation function.

The Code is as follows: Copy code

Var fs = require ("fs ");


How to use this fs? In this case, we need to query the official API of nodejs.


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

Basic file operations include creating, renaming, and deleting. Let's take a look at several examples.


[Create folder]

The Code is as follows: Copy code
// Newfolder. js
-----------------------------
// Load the file system module
Var fs = require ("fs ");

// Create a folder named a on drive C
Fs. mkdir ("c: a", function (err ){
If (! Err ){
Console. log ("operation successful! ");
} Else {
Console. log ("operation failed! ");
}
});

-----------------------------
[NOTE: If the folder contains err, an error message is displayed.]

Delete folder]

The Code is as follows: Copy code
// Removefolder. js
-----------------------------
Var fs = require ("fs ");

// Delete the folder in drive C
Fs. rmdir ("c: a", function (err ){
If (err ){
Console. log ("deletion failed! ");
} Else {
Console. log ("deleted successfully! ");
}
});

-----------------------------
[NOTE: If the deleted Folder does not exist, err will have an error message.]


[Rename a folder]
// Rename. js
-----------------------------

The Code is as follows: Copy code

Var fs = require ("fs ");

// Rename the folder to B
Fs. rename ("c: a", "C: B", function (err ){
If (err ){
Console. log ("Renaming failed! ");
} Else {
Console. log ("renamed successfully! ");
}
});

-----------------------------
[Note: The Folder does not exist, so err will have an error message.]


[Determine whether a file or folder exists]

The Code is as follows: Copy code

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

Var fs = require ("fs ");

// Determine whether the 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
(The value of this callback function is true or false)]

[File type determination] [fs. stat (), fs. lstat () and fs. fstat ()]
// Stat. js
-----------------------------

The Code is as follows: Copy code
Var fs = require ("fs ");

// Obtain the type of the folder
Fs. stat ("C: a", function (err, stat ){
If (err ){
Console. log ("the file does not exist! ");
} Else {
Console. log ("file:" + stat. isFile ());
Console. log ("folder:" + stat. isDirectory ());
}
});

-----------------------------
[
Other status functions:

Stats. isFile ()
Stats. isDirectory ()
Stats. isBlockDevice ()
Stats. isCharacterDevice ()
Stats. isSymbolicLink () (only valid for fs. lstat)
Stats. isFIFO ()
Stats. isSocket ()]

Related Article

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.