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

Answer: 
No. Browser-side javascript doesn't have permission to write to the client machine without a lot of security options having to be disabled
You'll have to turn to Flash, Java or Silverlight. In the case of Silverlight, you'll be looking at [Isolated Storage](http://msdn.microsoft.com/en-us/library/3ak841sy(VS.95).aspx). That will get you write to files in your own playground on the users disk. It won't let you write outside of your playground though.