//Path contains methods for processing pathsvarPath =require(' path ');//FS module contains all file manipulation functionsvarFS =require(' FS ');varSTR1 ="Home";varSTR2 ="/git";varSTR3 ="/nodeexploring";//Path stitchingvarDirpath = Path.join (str1, str2, STR3);Console. log (Dirpath);varSTR4 ="./io";//Path.resolve resolving multiple paths to an absolute path without a specificationvarDirPath2 = Path.resolve (Dirpath, STR4);Console. log (DirPath2)varSTR5 ="AAA";varSTR6 ="./bbb";//If the parsed path is not an absolute path, the current path is added to the front to make the absolute pathvarDirPath3 = Path.resolve (STR5, STR6);Console. log (DIRPATH3);varSTR7 ="/home/renlan/tem";//Calculates the relative path of a path to another pathvarDirPath4 = Path.relative (Dirpath, STR7);Console. log (DirPath4);varStr8 ="/home/renlan/tem.txt";//Get the directory where the files are locatedvarDirPath5 = Path.dirname (STR8);Console. log (DIRPATH5);//Gets the file name extensionvarExtname = Path.extname (STR8);Console. log (Extname);//Gets the filename, the second parameter has no file extensionvarFileName = Path.basename (Str8, extname);Console. log (FileName);//Gets the delimiter for the system pathUnix:'/',Windows:' \ \ 'varSep = path.sep;Console. log (Sep);/************ * Get or directory information * FS.STAT () returns a Stats object * * atime"Access Time"* Mtime"Modified Time"* CTime" Change Time"* Birthtime"Birth Time"* *************/FS.STAT ('./test ',function(Err, stats) {if(ERR)ThrowErr// Console. log (stats);});//Read the entire file Fs.readfile ('./test ',' UTF-8 ',function(Err, data) {if(ERR)ThrowErrConsole. log ("Read whole file:"+data);}); * Fs.write (FD, buffer, offset, length[, position], callback) * FD file pointer * Buffer Write data cache * Offset cache write to start Party * Position write to the beginning of the file whereNULLRepresents reading from the current location * Callback three parameters (err, written, buffer) * written number of bytes written * ***********************/fs.open('./test ',' A ', (err, fd) = = {varBuffer =NewBuffer ("Writing to this file"), offset =0, length = buffer.length, Position =NULL; Fs.write (fd, buffer, offset, length, position, (err, writen) = {if(ERR)ThrowErrConsole. log (' Write file successfully ');Console. log ("Write"+ writen +"bytes"); Fs.close (FD, (ERR) =>{if(ERR)ThrowErr }); });});/*************** *FS.Read(fd, buffer, offset, length, position, callback)*BufferWrite Cache of Data *OffsetWhere to start when the cache is written *lengthNumber of bytes Read *positionRead where the file started,NULLIndicates read from current location *CallbackThree parameters(err, bytesread, buffer)* ********************/FS.Open('./test ',' R ', (err, fd) = = {if(ERR)ThrowErrvarReadbuffer =NewBuffer (1024x768), Bufferoffset =0, bufferlength = readbuffer.length, fileposition =0; Fs.read (FD, Readbuffer, Bufferoffset, Bufferlength, Fileposition, (err, readbytes, buff ER) = = {if(ERR)ThrowErrConsole. log (' read '+ Readbytes +' bytes ');if(Readbytes >0) {//Slice the buffer and take out the part of the data read inConsole. log (Buffer.slice (0, readbytes));Console. log ("read file from buffer:"+buffer.slice (0, readbytes). toString ()); }//Close File Fs.close (FD, (ERR) =>{if(ERR)ThrowErr }); }); });
node. JS read-write file