nodejs>>fs
12520 ワード
fs File System
/* POSIX I/O 。 require('fs') 。 。
。 ,
。 , null undefined。
, , 。 try catch 。*/
fs.readFile(filename, encoding, callback)
filename
encoding , ,callback buffer
callback
fs.readFileSync(path, encoding);
filename
encoding , ,callback buffer
var fs = require('fs');
fs.readFile('a.txt', function(err, data){
if(err){
console.log(err);
}else{
console.log(data);//
}
});
fs.readFile('a.txt', 'utf-8', function(err, data){
if(err){
console.log(err);
}else{
console.log(data);//hello fs !
}
});
// ,err Error
var str = fs.readFileSync('a.txt');
console.log(str);//
var str = fs.readFileSync('a.txt', 'utf-8');
console.log(str);hello fs !
//fs.readFileSync fs.readFile , , ,fs , try catch
fs.open(path, flags, mode, callback);
path:
flags: {
'r':
'r+':
'w': ,
'w+': ,
'a': ,
'a+': , ( )
}
mode: , , 0666,
callback: fd,( , )
fs.open('../views', 'r+', function(err, data){
console.log(data);//3
});
fs.openSync(path, flags, [mode])
fs.read(fd, buffer, offset, length, position, callback);
fd buffer .
fd:
buffer:
offset: buffer
length: ,
position , , position null, 。
, (err, bytesRead, buffer), , 。
fs.readSync(fd, buffer, offset, length, position)
fs.read 。 bytesRead
var fs = require('fs');
fs.open('a.txt', 'r', function(err, fd){
if(err){
console.log(err);
return;
}
var buf = new Buffer(8);
fs.read(fd, buf, 0, 8, null, function(err, bytesRead, buffer){
if(err){
console.log(err);
return;
}
console.log('bytesRead: '+bytesRead);
console.log(buffer);
});
});
/*
bytesRead: 8
*/
fs.rename(oldPath, newPath, callback)
oldPath newPath,callback err
fs.renameSync(oldPath, newPath)
,
fs.rename('a.txt', 'aa.txt', function(err){});
fs.renameSync('aa.txt', 'a.txt');
fs.ftruncate(fd, len, callback)
fd len, >len, , len, ,
fs.chownSync(path, uid, gid)
(path)
fs.fchown(fd, uid, gid, callback)
(fd)
fs.fchownSync(fd, uid, gid)
( )
fs.lchown(path, uid, gid, callback)
( )
fs.lchownSync(path, uid, gid)
( )
fs.chmod(path, mode, callback)
(path)
fs.chmodSync(path, mode)
( )
fs.fchmod(fd, mode, callback)
(fd)
fs.fchmodSync(fd, mode)
(fd )
fs.lchmod(path, mode, callback)
( )
fs.lchmodSync(path, mode)
( )
fs.stat(path, callback(err, stats))
fs.stat('../api', function(err, stats){
if(err){
console.log(err);
return ;
}
console.log(stats);
})
/*
{ dev: 0,
mode: 16822,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
ino: 0,
size: 0,
atime: Wed Oct 15 2014 16:29:10 GMT+0800 ( ),
mtime: Wed Oct 15 2014 16:29:10 GMT+0800 ( ),
ctime: Sat Oct 04 2014 16:05:10 GMT+0800 ( ) }
*/
fs.statSync(path);
( )
fs.lstat(path, callback)
( )
fs.fstat(fd, callback)
(fd)
fs.lstatSync(path)
( )
fs.fstatSync(fd)
(fd )
fs.link(srcpath, dstpath, callback(err));
// srcpath , dstpath,dstpath srcpatj
//link hard link symlink symbolic link ,
fs.link('a.txt', 'current', function(err) {
if(err) {
console.log(err);
return ;
}
fs.readFile('current', 'utf8', function(err, data) {
// a.txt current , a.txt
var currentLog = data;
console.log(data);
});
});
fs.linkSync(srcpath, dstpath)
fs.symlink(srcpath, dstpath, [type], callback)
// dstpath, srcpath
fs.symlink('a.txt', 'current', function(err) {
if(err) {
console.log(err);
return ;
}
fs.readFile('current', 'utf8', function(err, data) {
// data is the contents of a.txt
var currentLog = data;
console.log(data);
});
});
fs.symlinkSync(srcpath, dstpath, [type])
fs.readlink(path, [callback])
fs.realpath(path, [callback])
fs.unlink(path, [callback])
fs.readlinkSync(path)
fs.realpathSync(path)
fs.unlinkSync(path)
// readlink, (err,resolvedPath),resolvedPath 。
// 3 : 1、readlink: 2、realpath: 3、unlink:
fs.rmdir(path, [callback])
fs.mkdir(path, mode, [callback])
fs.readdir(path, [callback])
fs.rmdirSync(path)
fs.mkdirSync(path, mode)
fs.readdirSync(path)
//rmdir: mkdir: readdir: 。
// (err, files), files ('.' '..' )。
fs.mkdir('aa.js', function(err, files){// aa.js,
console.log(files);
fs.rmdir('aa.js', function(err, files){// aa.js
console.log(files);
fs.readdir('../', function(err, files){//
console.log(files);
});
});
});
fs.close(fd, callback)
// fd
fs.closeSync(fd)
fs.utimes(path, atime, mtime, callback)
fs.utimesSync(path, atime, mtime)
fs.futimes(path, atime, mtime, callback)
fs.futimesSync(path, atime, mtime)
// ,2 ,utimes , ,futimes 。
fs.write(fd, buffer, offset, length, position, [callback])
fs.writeSync(fd, buffer, offset, length, position)
fs.writeSync(fd, str, position, encoding='utf8')
fs.write buffer fd ,
/*offset length 。
position , position null,
(err, written), written
fs.write() , 。
: */
fs.writeFile(filename, data, [options], callback)
// data
filename {String}
data {String | Buffer}
options {
encoding {String | Null} default = 'utf8'
mode {Number} default = 438 (aka 0666 in Octal)
flag {String} default = 'w'
}
callback {Function}
fs.writeFile('message.txt', 'Hello Node', function (err) {
if (err) throw err;
console.log('It\'s saved!'); //
});
fs.writeFileSync(filename, data, [options])
fs.appendFile(filename, data, [options], callback)
// data
filename {String}
data {String | Buffer}
options {
encoding {String | Null} default = 'utf8'
mode {Number} default = 438 (aka 0666 in Octal)
flag {String} default = 'w'
}
callback {Function}
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!'); //
});
fs.appendFileSync(filename, data, [options])
fs.watchFile(filename, [options], listener)
fs.unwatchFile(filename)
//fs.watchFile filename , listener .
// [options] , options , : persistent , interval ,
// { persistent: true, interval: 0 }。 listener (curr, prev), , fs.Stat 。
//fs.unwatchFile filename 。
fs.watch(filename, [options], [listener])
// ,filename 。
// . options persistent ,
//persistent boolean 。persistent “ ” { persistent: true }.
// (event, filename)。 event 'rename'( ) 'change'( ), filename 。
//fs.watch , ,
/* Linux , inotify。
BSD ( OS X), kqueue。
SunOS ( Solaris SmartOS), event ports。
Windows , ReadDirectoryChangesW。*/
/* , fs.watch 。 , ( NFS, SMB ) , , 。*/
// fs.watchFile, 。
/* filename ( Linux Windows )。 ,filename 。
, filename , filename null if 。*/
fs.watch('somedir', function (event, filename) {
console.log('event is: ' + event);
if (filename) {
console.log('filename provided: ' + filename);
} else {
console.log('filename not provided');
}
});
fs.exists(path, callback)
fs.existsSync(path)
// 。 callback (true) (false)。
fs.exists('/etc/passwd', function (exists) {
util.debug(exists ? " " : " !");
});
Class: fs.Stats
fs.stat() fs.lstat()
stats.isFile()
stats.isDirectory()
stats.isBlockDevice()
stats.isCharacterDevice()
stats.isSymbolicLink() ( fs.lstat() )
stats.isFIFO()
stats.isSocket()
{ dev: 2049,
ino: 305352,
mode: 16877,
nlink: 12,
uid: 1000,
gid: 1000,
rdev: 0,
size: 4096,
blksize: 4096,
blocks: 8,
atime: '2009-06-29T11:11:55Z',
mtime: '2009-06-29T11:11:40Z',
ctime: '2009-06-29T11:11:40Z' }
fs.createReadStream(path, [options])
options :
{ flags: 'r', encoding: null, fd: null, mode: 0666, bufferSize: 64 * 1024 }
, options start end 。
start end ( ), 0 。
fs.createWriteStream(path, [options])
options :
{ flags: 'w', encoding: null, mode: 0666 }
Class: fs.ReadStream
ReadStream (Readable Stream).
: 'open'
fd { } ReadStream 。
ReadStream 。
fs.createWriteStream(path, [options])
WriteStream ( Writable Stream).
options :
{ flags: 'w',encoding: null,mode: 0666 }
options start 。 flags r+ w.
Class: fs.WriteStream
WriteStream (Writable Stream).
: 'open'
fd { } WriteStream 。
WriteStream 。
file.bytesWritten
。 。
Class: fs.FSWatcher
fs.watch() 。
watcher.close()
fs.FSWatcher 。
: 'change'
event { } fs
filename { } (if relevant/available)
。 , fs.watch。
: 'error'
error {Error }
asd