Question: Can anybody give some sample code to read and write a file using JavaScript?

Answer: 
here's the mozilla proposal

<http://www-archive.mozilla.org/js/js-file-object.html>

this is implemented with a compilation switch in spidermonkey, and also in adobe's extendscript. Additionally (I think) you get the File object in firefox extensions.

rhino has a (rather rudementary) readFile function
<https://developer.mozilla.org/en/Rhino_Shell>

for more complex file operations in rhino, you can use java.io.File methods.

you won't get any of this stuff in the browser though. For similar functionality in a browser you can use the SQL database functions from HTML5, clientside persistence, cookies, and flash storage objects.
For Firefox:

```
var file = Components.classes["@mozilla.org/file/local;1"].
       createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/home");

```

See <https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O>

For others, check out the [TiddlyWiki](http://www.tiddlywiki.com/) app to see how it does it.