Read and Write

What differentiates JS on the server from the client is its ability to do stuff out of the sandboxed environment of the browser, like reading and writing files on your hard drive. Spooky? Fear not...

I can do whatever I want in my own server so why not reading and writing files? Maybe I need to write a log or read a config file. But, how? you may ask.

There is a File object which can be used to read and write files, and also to move, copy, delete and do any kind of file manipulation. But for this post, read and write will suffice, so head on:

writeFile('test1.txt','lorem ipsum');
txt=readFile('test1.txt');
response.write('File1: '+txt); // lorem ipsum

writeFile('test2.txt','my test file');
txt=readFile('test2.txt');
response.write('File2: '+txt); // my test file

function writeFile(name,text){
  var file = new File(name);
  file.open('w');  // write only
  file.write(text);
  file.close();
}

function readFile(name){
  var text,file;
  file = new File(name);
  file.open('r');  // read only
  text = file.read();
  file.close();
  return text;
}

Self explanatory, it makes my job easier for there is nothing else to say.

End of post.