One, open and close files//behave differently in different operating systems
- var fd=fs.open (Path,flags,[mode],callback)//Returns an integer representing the open file object
- Fs.close (Fd,callback)
Attached: Flags parameter table, indicating the mode of open file
- ' R ' -Open file for reading. An exception occurs if the file is does not exist.
‘r+‘
-Open file for reading and writing. An exception occurs if the file is does not exist.
‘rs+‘
-Open file for reading and writing in synchronous mode. Instructs the operating system to bypass the local file system cache.
This is a primarily useful for opening files on NFS mounts as it allows you to skip the potentially stale local cache. It has a very real impact on I/O performance so don ' t use this flag unless you need it.
Note that this doesn ' t turn to fs.open()
a synchronous blocking call. If that's what's your want then should is usingfs.openSync()
‘w‘
-Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
‘wx‘
-like but ‘w‘
fails if path
exists.
‘w+‘
-Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
‘wx+‘
-like but ‘w+‘
fails if path
exists.
‘a‘
-Open file for appending. The file is created if it does not exist.
‘ax‘
-like but ‘a‘
fails if path
exists.
‘a+‘
-Open file for reading and appending. The file is created if it does not exist.
‘ax+‘
-like but ‘a+‘
fails if path
exists.
Second, write the file
1. Simple File Writing
Fs.writefile (Filename,data,[opations],callback)
- Finename: Specify File
- Data: Specifies the string or buffer object that will be written to the file
- Opations: Is an object that can contain the encoding, mode, and flag properties that define the string encoding and the patterns and flags used when opening the file
- Callback: The function that is called when the file write is completed
2. synchronous file Writing
var fd=fs.opnesync (Path,flags[,mode])
Fs.writesync (Fd,buffer,offset,length[,position])
Fs.writesync (fd,data[,position][,encoding])
Fs.closesync (FD)
- Path: Specify file
- Flags: Specifies the mode in which the file is opened
- Mode: Sets the access pattern for the file
- FD: Integer, representing the file object, typically the return value of the Fs.open () function
- Data: Write to
- Buffer: Buffered data
- offset/length
:
Determines where to start writing to buffer.
3. Writing Files asynchronously
- Fs.open (path, flags[, mode], callback)//asynchronous write to file with callback (ERR,FD) function
- Fs.write (fd, buffer, offset, length[, position], callback)
- Fs.write (FD, data[, position[, encoding]], callback)
- Fs.close (FD)
4. Stream file Write
- var filewritestream=fs.createwritestream (Path[,options])
- Filewritestream.write (data)
- Filewritestream.on ("Close", Handler)//end method is called after execution
- Filewritestream.end ()
Third, read the file (IBID.)
1. Simple file reading
- Fs.readfile (file[, Options], callback)
2. synchronous file Reading
- Fs.readsync (fd, buffer, offset, length, position)
3. Asynchronous file read
- Fs.read (fd, buffer, offset, length, position, callback)
4. streaming file read
- Fs.createreadstream (path[, Options])
- Data event and Close event
Iv. Other File system tasks
1, Fs.stat (path, callback)//Get file information
- Stats.isfile ()//If the entry is a file, returns True
stats.isDirectory() //如果条目是一个目录,返回true
stats.isBlockDevice() //
stats.isCharacterDevice() //
stats.isSymbolicLink()
(Only valid with Fs.lstat ())//
stats.isFIFO() //
stats.isSocket() 如果条目是一个套接字,返回true
2. List files (all subdirectories of Path path are listed, stored as arrays)
- Fs.readdir (path[, Options], callback)
3. Delete Files
- Fs.unlink (Path,callback)
4. truncate files
- Fs.truncate (Path, Len, callback)//intercept Len length from the file and discard it later
5. Create and delete directories
- Fs.mkdir (path[, mode], callback)//established
- Fs.rmdir (path, callback)//delete
6. renaming files and directories
- Fs.rename (OldPath, NewPath, callback)
7. monitor file changes into
- Fs.watchfile (filename[, Options], listener)//options contains peristent (whether persistent) and interval (interval check) properties
File System (FS module)