Node.js内置的fs模块就是文件系统模块,负责读写文件,提供了异步和同步的方法。
异步读文件
'use strict';
var fs = require('fs');
fs.readFile('sample.txt', 'utf-8', function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
sample.txt文件必须在当前目录下,且文件编码为utf-8
正常读取时,err参数为null,data参数为读取到的String
取发生错误时,err参数代表一个错误对象,data为undefined。
读取图片文件
'use strict';
var fs = require('fs');
fs.readFile('sample.png', function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
console.log(data.length + ' bytes');
}
});
回调函数的data参数将返回一个Buffer对象
Buffer对象转换成String:
// Buffer -> String
var text = data.toString('utf-8');
console.log(text);
String转换成Buffer:
// String -> Buffer
var buf = new Buffer(text, 'utf-8');
console.log(buf);
同步读文件
'use strict';
var fs = require('fs');
var data = fs.readFileSync('sample.txt', 'utf-8');
console.log(data);
try {
var data = fs.readFileSync('sample.txt', 'utf-8');
console.log(data);
} catch (err) {
// 出错了
}
写文件
'use strict';
var fs = require('fs');
var data = 'Hello, Node.js';
fs.writeFile('output.txt', data, function (err) {
if (err) {
console.log(err);
} else {
console.log('ok.');
}
});
如果传入的数据是String,默认按UTF-8编码写入文本文件,如果传入的参数是Buffer,则写入的是二进制文件。
同步写文件
'use strict';
var fs = require('fs');
var data = 'Hello, Node.js';
fs.writeFileSync('output.txt', data);
stat
'use strict';
var fs = require('fs');
fs.stat('sample.txt', function (err, stat) {
if (err) {
console.log(err);
} else {
// 是否是文件:
console.log('isFile: ' + stat.isFile());
// 是否是目录:
console.log('isDirectory: ' + stat.isDirectory());
if (stat.isFile()) {
// 文件大小:
console.log('size: ' + stat.size);
// 创建时间, Date对象:
console.log('birth time: ' + stat.birthtime);
// 修改时间, Date对象:
console.log('modified time: ' + stat.mtime);
}
}
});
https://github.com/michaelliao/learn-javascript/tree/master/samples/node/fs
read_image_file_async.js
'use strict'
const fs=require('fs');
console.log('>>>> BEGIN >>>>');
fs.readFile(__dirname+'/'+'sample.png',function(err,data){
//'sample.png' => d:\\workspace\\js\\sample.png
//__dirname+'/'+'sample.png' => d:\\workspace\\js\\test\\sample.png
if(err){
console.log(err);
}else{
console.log(data);
console.log(data.length+' bytes');
}
});
console.log('>>> END >>>');
>>>> BEGIN >>>>
>>> END >>>
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 2c 00 00 01 2c 08 06 00 00 00 79 7d 8e 75 00 00 20 00 49 44 41 54 78 9c ec bd bd ba 2d 49 92 ... >
73770 bytes
read_image_file_sync.js
'use strict';
// read from 'sample.png'
const fs = require('fs');
console.log('>>> BEGIN >>>')
var data = fs.readFileSync(__dirname+'/'+'sample.png');
console.log(data);
console.log(data.length + ' bytes');
console.log('>>> END >>>')
>>>> BEGIN >>>>
<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 2c 00 00 01 2c 08 06 00 00 00 79 7d 8e 75 00 00 20 00 49 44 41 54 78 9c ec bd bd ba 2d 49 92 ... >
73770 bytes
>>>> END >>>>
read_text_file_async.js
'use strict';
// read text from 'sample.txt'
const fs = require('fs');
console.log('>>> BEGIN >>>')
fs.readFile(__dirname+'/'+'sample.txt', 'utf-8', function (err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
console.log('>>> END >>>')
>>>> BEGIN >>>>
>>>> END >>>>
Nodejs教程:File I/O is provided by simple wrappers around standard POSIX functions. To use this module do require('fs'). All the methods have asynchronous and synchronous forms.
read_text_file_sync.js
'use strict';
// read text from 'sample.txt'
const fs = require('fs');
console.log('>>> BEGIN >>>')
var data = fs.readFileSync('sample.txt', 'utf-8');
console.log(data);
console.log('>>> END >>>')
use_buffer.js
'use strict';
// read binary data from 'sample.txt'
const fs = require('fs');
var data = fs.readFileSync('sample.txt')
console.log(data);
console.log(data.length + ' bytes');
console.log('First three bytes: ' + data[0] + ', ' + data[1] + ', ' + data[2]);
// Buffer -> String
var text = data.toString('utf-8');
console.log(text);
// String -> Buffer
var buf = new Buffer(text, 'utf-8');
console.log(buf);
<Buffer 4e 6f 64 65 6a 73 e6 95 99 e7 a8 8b ef bc 9a 46 69 6c 65 20 49 2f 4f 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 79 20 73 69 6d 70 6c 65 20 77 72 61 70 ... >
181 bytes
First three bytes: 78,111,100
Nodejs教程:File I/O is provided by simple wrappers around standard POSIX functions. To use this module do require('fs'). All the methods have asynchronous and synchronous forms.
<Buffer 4e 6f 64 65 6a 73 e6 95 99 e7 a8 8b ef bc 9a 46 69 6c 65 20 49 2f 4f 20 69 73 20 70 72 6f 76 69 64 65 64 20 62 79 20 73 69 6d 70 6c 65 20 77 72 61 70 ... >
use_stat.js
'use strict';
const fs = require('fs');
fs.stat('sample.txt', function (err, stat) {
if (err) {
console.log(err);
} else {
console.log('isFile: ' + stat.isFile());
console.log('isDirectory: ' + stat.isDirectory());
if (stat.isFile()) {
console.log('size: ' + stat.size);
console.log('birth time: ' + stat.birthtime);
console.log('modified time: ' + stat.mtime);
}
}
});
isFile: true
isDirectory: false
size: 181
birth time: Wed Jul 05 2017 14:05:20 GMT+0800 (中国标准时间)
modified time: Wed Jul 05 2017 14:05:20 GMT+0800 (中国标准时间)
write_text_file_async.js
'use strict';
// write text to 'output.txt'
const fs = require('fs');
console.log('>>> BEGIN >>>')
var data = 'Hello, Node.js';
fs.writeFile('output.txt', data, function (err) {
if (err) {
console.log(err);
} else {
console.log('ok.');
}
});
console.log('>>> END >>>')
write_text_file_sync.js
'use strict';
// write text to 'output.txt'
const fs = require('fs');
console.log('>>> BEGIN >>>')
var data = 'Hello, Node.js';
fs.writeFileSync('output.txt', data);
console.log('>>> END >>>')