Friday, March 25, 2011

reading writing files with javascript to local file / computer

this is quite interesting. writing/reading local files using javascript and mozilla:

/* Code from http://www.captain.at/programming/xul/ */
/* Filename: writeToFile.js */

writeToFile("C:\\Users\\me\\Documents\\myFile.txt","Hello World!!\r\nThis is the second line...\r\n");
alert(readFromFile("C:\\Users\\me\\Documents\\myFile.txt"));


function writeToFile(filename, data) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("Permission to save file was denied.");
}
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( filename );
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 );

// Open flags
var PR_RDONLY = 0x01
var PR_WRONLY = 0x02
var PR_RDWR = 0x04
var PR_CREATE_FILE = 0x08
var PR_APPEND = 0x10
var PR_TRUNCATE= 0x20
var PR_SYNC = 0x40
var PR_EXCL = 0x80

/*
** File modes ....
**
** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
** The 'mode' argument may be ignored by PR_Open on other platforms.
**
** 00400 Read by owner.
** 00200 Write by owner.
** 00100 Execute (search if a directory) by owner.
** 00040 Read by group.
** 00020 Write by group.
** 00010 Execute by group.
** 00004 Read by others.
** 00002 Write by others
** 00001 Execute by others.
**
*/

outputStream.init( file, PR_RDWR | PR_CREATE_FILE | PR_APPEND, 420, 0 );
var result = outputStream.write( data, data.length );
outputStream.close();
}

var data;
function readFromFile(filename, data) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("Permission to read file was denied.");
}
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( filename );
if ( file.exists() == false ) {
alert("File does not exist");
}
var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance( Components.interfaces.nsIFileInputStream );
is.init( file,0x01, 00004, null);
var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
.createInstance( Components.interfaces.nsIScriptableInputStream );
sis.init( is );
var output = sis.read( sis.available() );
data = output;
return(data);
//document.getElementById('blog').value = output;
}

taken from a forum at: http://forum.iopus.com/viewtopic.php?f=11&t=5267

1 comment:

  1. If you can Please help me. How would this code be modified to truncate or append a text file (add text to the bottom of existing text). I use Firefox Custom Buttons (add-on) and I have been for over three weeks trying to find working code to do so. I have 85% of the code done beside the appending text issue

    Custom Buttons :: Add-ons for Firefox
    https://addons.mozilla.org/en-US/firefox/addon/custom-buttons/

    posting link to code I have do far

    Re: Is there code to append a Text file

    http://custombuttons.sourceforge.net/forum/viewtopic.php?f=2&t=124#p469

    ReplyDelete

cancel script completely on ctrl-c

I found this question interesting: basically how to cancel completely a script and all child processes : You do this by creating a subro...