process module
at the time of use without the need require () function to load the module, it can be used directly.
fs module
, when in use, must () function to load the module through the require, before use.
The reason: process modules are global module, the module rather than fs global module. Global module can be used directly, rather than via the global module needs require ( '') to load the module.
First, the implementation file write operations
1 // 1. Load module file operation, fs module 2 var FS = the require ( 'FS' ); . 3 . 4 // 2. implement file write operations . 5 var MSG = "Hello World, Hello world" ; . 6 . 7 / / call fs.writeFile () to write files . 8 // fs.writeFile () is an asynchronous method . 9 // fs.writeFile ( 'write the file path' and 'data to be written', 'document encoding formats' 'callback') 10 fs.writeFile ( 'hello.txt', MSG, 'UTF8', (err) => { . 11 // if err === null, indicates that the file is written 12 // long as there is not err null, it means write to file failed! 13 IF (ERR) { 14 console.log ( 'Error writing to file pull-specific error:!' + ERR) 15 } the else { 16 console.log ( 'OK' ); . 17 } 18 is });
Second, the implementation file read operation
1 // 1. Load module file operation, fs module 2 var FS = the require ( 'FS' ); . 3 . 4 // 2. calls fs.readFile () method to read the file . 5 // fs.readFile ( 'read path to the file ',' file encoding format '' callback ') 6 // read the document, if the transmission encoding format, the callback function will be converted to the default string data, or data parameters Buffer is a data object which is stored in a one byte (byte array understood) 7 // converts Buffer string object, call toString () method . 8 . 9 fs.readFile ( './ hello.txt ',' UTF8 ', (ERR, Data) => { 10 IF (ERR) { . 11 the throw ERR; 12 is } the else { 13 is the console.log (Data); 14 } 15 });
Routing Problem 1. Read file
./ relative path, relative path node command execution, rather than relative to the js file is being performed to find hello.txt
./ relative path to solve the problem of reading in the file
Solution
: Use __dirname, __ filename
__dirname: said directory js file currently being executed is located
___filename: express, full path to the js file that is currently being executed
// console.log(__dirname); C:\Users\zhuyujie\Desktop\nodejs\1.fs
// console.log(__filename); C:\Users\zhuyujie\Desktop\nodejs\1.fs\2.fs_readFile.js
Note:
__dirname, not global filename ___
Path above code should be modified to:
. 1 var FS = the require ( 'FS' ); 2 . 3 // var filename = __dirname + '\\' + 'hello.txt' (not recommended) . 4 . 5 // recommended path by a path module for splicing . 6 var path = the require ( 'path' ); . 7 . 8 var filename = path.join (__ dirname, 'hello.txt' ) . 9 10 // the console.log (filename); . 11 12 is fs.readFile (filename, 'UTF8', (ERR, Data) => { 13 is IF (ERR) { 14 the throw ERR; 15 } 16 the console.log (Data); . 17 18 });
Third, create a folder
. 1 var FS = the require ( 'FS' ); 2 . 3 fs.mkdir ( 'Test-mkdir', (ERR) => { . 4 IF (ERR) { . 5 the console.log ( 'create the directory wrong, details' + ERR ); . 6 } the else { . 7 the console.log ( 'create the directory success' ); 8 } 9 });