hi friends,
How can i write an read a text file in a JavaScript?
Thanks
hi friends,
How can i write an read a text file in a JavaScript?
Thanks
You can not access your file system with Java Script, so unfortunately you can't
You can't. JavaScript in the browser has no access to the user's filesystem, by design.
You need to run the JS in a host environment that provides an API for accessing the file system.
If you are on Windows, then you can use WSH to achieve this.
JS running a browser, under normal security conditions, cannot access the file system.
In FF 3.6 it is possible, see my technical example at http://www.bruechner.de/md5file/js/
If you are using Firefox, this may help.
//Your text file location on system
var savefile = "c:\\yourtextfile.txt";
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( savefile );
if ( file.exists() == false ) {
alert( "Creating file... " );
file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
}
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance( Components.interfaces.nsIFileOutputStream );
outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var output = "Your text here";
var result = outputStream.write( output, output.length );
outputStream.close();
alert("Done");
}
catch (e) {
alert("Some error occured");
}
It worked for me, hope works for you as well :)