Front child shoes are aware that JavaScript is not authorized to manipulate disk files, server children's shoes have always been very despised. But Nodejs let us front and proud Ah, the recent learning node, its powerful function makes people unusually excited and excited. Today I learned how to read and write files.
First of all, we need to introduce the FS module, which is Nodejs.
var fs=require ("FS");
Specific reference Nodejs API:http://www.w3cfuns.com/tools.php?mod=booknodejs
The main use of two methods:
1, fs.readfile (filename, [encoding], [callback])
This is the asynchronous read file, filename is the file path, encoding is the encoding format, callback is the callback function.
Read all the contents of a file asynchronously, as shown in the following example:
Fs.readfile ('/etc/passwd ', function (err, data) {
if (err) throw err;
Console.log (data);
Here I am using a local test file:
function ReadFile () {
console.log ('--------start reading file--------');
var fs = require (' FS ');
Fs.readfile (' test.txt ', ' utf-8 ', function (err, data) {
if (err) {
Console.log (read failed);
} else {
Console.log (data);
Return Data
}
});
Console.log ('--------read End--------');
}
2, Fs.writefile (filename, data, encoding= ' UTF8 ', [callback])
Write file:
function WriteFile (data) {
fs.writefile ("Test2.txt", Data,function (Error) {
if (error) {
throw error;
} else{
console.log ("File Saved");}
Error code: Copyfile.js file
var fs=require ("FS");
function ReadFile () {
console.log ('--------start reading file--------');
var fs = require (' FS ');
Fs.readfile (' test.txt ', ' utf-8 ', function (err, data) {
if (err) {
Console.log (read failed);
} else {
Console.log (data);
Return Data
}
});
Console.log ('--------read End--------');
}
function WriteFile (data) {
fs.writefile ("Test2.txt", Data,function (Error) {
if (error) {
throw error;
} else{
console.log ("File Saved");}
function CopyFile () {
var txt=readfile ();
WriteFile (TXT);
}
CopyFile ();
The result of running node Copyfile.js at the terminal is as follows:
Precautions:
1, file encoding, at the beginning I directly in the local to create a TXT document, read the results are always aaaaaaa this, the results found that with the editor Open is the time inside is garbled, second, it is best to bring encoding, otherwise it will be read in accordance with the buffer.
2, synchronous implementation issues.
The code above is problematic, and I write it separately from the way I read and write the file. Originally wanted to copy the contents of the Test.txt file to the test2.txt, but read the file is asynchronous execution, that is to say who does not know what the month of the implementation of the horse, so test.txt the result is undefined.
The correct way to do this is to write the file after the read is finished:
var fs=require ("FS");
function CopyFile () {
console.log ('--------start reading file--------');
var fs = require (' FS ');
Fs.readfile (' test.txt ', ' utf-8 ', function (err, data) {
if (err) {
Console.log (read failed);
} else {
WriteFile (data) return
data;
}
);
Console.log ('--------read End--------');
}
function WriteFile (data) {
console.log (data);
Fs.writefile ("Test2.txt", data, ' UTF8 ', function (error) {
if (error) {
throw error;
} else{
console.log ("File Saved");}
CopyFile ();
The above is the entire content of this article, I hope to help you learn.