Jerry Jacob's Blog

Reading and Writing from/ to a file using javascript

Posted in Uncategorized by jerlinworld on October 12, 2010

var hello='Hello '; 
var world='World'; 

function WriteToFile() { 
var filename = 'c://hello.txt'; 
var fso = new ActiveXObject('Scripting.FileSystemObject'); 
if (fso.FileExists(filename)) { 
var a, forAppend, file; 
forAppend = 8; 
file = fso.OpenTextFile(filename, forAppend, false); 
file.WriteLine(world); 
file.WriteLine(hello); 
} 
else { 
var file = fso.CreateTextFile(filename, true); 
file.WriteLine(hello); 
file.WriteLine(world); 
} 
file.Close(); 
} 

function ReadFromFile() { 
var filename = 'c://hello.txt'; 
if (confirm('Do you want to see what we put on your computer?')) { 
var fso, a, forRead; 
forRead = 1; 
fso = new ActiveXObject('Scripting.FileSystemObject'); 
file = fso.OpenTextFile(filename, forRead, false); 
var world = file.readline(); 
var hello = file.readline(); 
file.Close(); 
document.write(world + '<br>'); 
document.write(hello); 
} 
} 
Advertisements