1 I. Direct operation of files2 The simplest of two fs.readfile and Fs.writefile3 Example: The function of this program is to convert a file of a larger JSON format into a file that you want to format. 4 varFS = require (' FS ');5Fs.readfile ('./json.json ',function(err,data) {6 if(ERR)Throwerr;7 8 varJsonobj =json.parse (data);9 varSpace = ';Ten varNewLine = ' \ n '; One varchunks = []; A varLength = 0; - - for(vari=0,size=jsonobj.length;i<size;i++){ the varone =Jsonobj[i]; - //What is value you want - varvalue1 = one[' value1 ']; - varvalue2 = one[' value2 ']; + .... - varValue = value1 +space+value2+space+.....+NewLine; + varBuffer =NewBuffer (value); A chunks.push (buffer); atLength + =buffer.length; - } - - varResultbuffer =NewBuffer (length); - for(vari=0,size=chunks.length,pos=0;i<size;i++){ - chunks[i].copy (resultbuffer,pos); inpos + =chunks[i].length; - } to +Fs.writefile ('./resut.text ', Resultbuffer,function(err) { - if(ERR)Throwerr; theConsole.log (' has finished '); * }); $ Panax Notoginseng }); - the principle is that the file data is read into memory all at once, the advantage is that the next is in memory operation, the speed will be very fast. But the disadvantage is also obvious, that is, when the file is very large, it will cause memory overflow. the two. Using File streams +2.1read files, API meet: Fs.createreadsream and Fs.createwriterstream A The following code implements the ability to copy a picture through a file stream: the varFS = require (' FS '); + varRoption = { -Flags: ' R ', $Encoding:NULL, $mode:0666 - } - the varWoption = { -Flags: ' A ',WuyiEncodingNULL, themode:0666 - } Wu - varFilereadstream = Fs.createreadstream ('./myjpg.jpg '), roption); About varFilewritestream = Fs.createwritestream ('./new_myjpg.jpg '), woption); $ -Filereadstream.on (' Data ',function(data) { - filewritestream.write (data); - A }); + theFilereadstream.on (' End ',function(){ -Console.log (' Readstream end '); $ filewritestream.end (); the }); the Here we add a function that is very useful in the flow: pipe, which is used to connect the current readable stream with another writable stream. The data in the readable stream is automatically written to the writable stream. It is very convenient to use, still realizes the function in the above example: the varFS = require (' FS '); the - varFilereadstream = Fs.createreadstream ('./myjpg.jpg ')); in varFilewritestream = Fs.createwritestream ('./new_myjpg.jpg ')); the filereadstream.pipe (filewritestream); the AboutFilewritestream.on (' Close ',function(){ theConsole.log (' Copy over '); the }); the This function makes it easy to implement a static resource server: + varHTTP = require ("http"); - varFS = require ("FS"), the varPath = require ("path"), Bayi varurl = require ("url"); the the varServer = Http.createserver (function(req, res) { - varpathname =Url.parse (req.url). Pathname; - Console.log (pathname); the varfilepath = Path.join ("./tmp", "Wwwroot", pathname); the Console.log (filepath); the varStream = Fs.createreadstream (filepath, {flags: "R", encoding:NULL}); theStream.on ("Error",function() { -Res.writehead (404); the res.end (); the }); the Stream.pipe (res);94 }); theServer.on ("Error",function(Error) { the Console.log (error); the }); 98Server.listen (8088,function(){ AboutConsole.log (' Server Listen on 8088 '); -});
Nodejs Read and write files