File IO

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 22:26, 24 September 2004
Asqueella (Talk | contribs)
(subfiles/subdirectories enumeration code snippet)
<-- Previous diff
Revision as of 22:31, 24 September 2004
Asqueella (Talk | contribs)
(remove "Traverse directories", 'cause I'm not going to write that)
Next diff -->
Line 103: Line 103:
array.push(entry); array.push(entry);
}</pre> }</pre>
-===Traverse directories=== 

Revision as of 22:31, 24 September 2004



This article describes local file input/output in Javascript.

You access the filesystem using Mozilla XPCOM components. The list of components used for local IO is available at

Available libraries

There are a few JavaScript wrappers for IO XPCOM components. See JSLib and MonkeeSage's IO module. MonkeeSage's module is much smaller and very easy to use (simple examples are included in the module).

Snippets using XPCOM directly

Creating a file object ("opening" files)

var file = Components.classes[";1"].

Note: the path passed to initWithPath() should be in 'native' form (eg. "C:\\Windows"). If you need to use file:// URIs as initializers, see below.

Getting special files

// get profile directory
var file = Components.classes[";1"].
	get("ProfD", Components.interfaces.nsIFile);

Here are some strings you can put in place of "ProfD" (stolen from MonkeeSage's IO module comments)

String Meaning
ProfD profile directory
DefProfRt user (e.g., /root/.mozilla)
UChrm %profile%/chrome
DefRt installation
PrfDef %installation%/defaults/pref
ProfDefNoLoc %installation%/defaults/profile
APlugns %installation%/plugins
AChrom %installation%/chrome
ComsD %installation%/components
CurProcD installation (usually)
Home OS root (e.g., /root)
TmpD OS tmp (e.g., /tmp)

Look in the Source for other strings available: [1] [2].

nsIFile and path strings

You can use nsIFile::path to get platform-specific path string, eg. C:\Windows\System32 or /usr/share. If you want to get a file:// URL of a file or an nsIFile from file:// URL, you need to use nsIFileProtocolHandler:

// file is nsIFile
var URL = Components.classes[";1?name=file"]. 

Note: do not use nsILocalFile::persistentDescriptor to get the file path!

Also note that generally you don't need to use nsIFile::path. Use nsIFile directly wherever possible. An example below shows how you should save a path in user prefs.

Saving path in a userpref

This snippet shows the right way to save a file path in user preferences:

// |file| is nsILocalFile
var prefs = Components.classes[";1"]. 
prefs.setComplexValue("filename", Components.interfaces.nsILocalFile, file);

/* You can read it later with this statement:
var file = prefs.getComplexValue("filename", Components.interfaces.nsILocalFile);

Navigating with nsIFile

Get a file in given directory

Assume, file is an nsIFile pointing to some directory (f.e. a user profile directory). You can use this code: file.append("myfile.txt"); - to make file point to myfile.txt inside that directory.

Notes: avoid using dir.path+"\\"+"myfile.txt", as it is not cross-platform code. Using something like ((\\/) != -1) ? path + "\\" : path + "/") + "myfile.txt"; is possible, but the nsIFile::append() is much easier to read and is guaranteed to work on all platforms Mozilla itself works.

Enumerating files and directories in given dir

The snippet below makes an array of |nsIFile|s corresponding to sub-files/subdirectories of given directory.

// file is the given directory (nsIFile)
var entries = file.directoryEntries;
var array = [];
  var entry = entries.getNext();

Reading from a file


// file is nsIFile
var data = "";
var fstream = Components.classes[";1"]
var sstream = Components.classes[";1"]
fstream.init(file, 1, 0, false);
data +=;

Line by line

// open an input stream from file
var istream = Components.classes[";1"]
istream.init(file, 0, 1, null);

// read lines into array
var line = {}, lines = {}, hasmore;
do {
  hasmore = istream.readLine(line);
} while(hasmore);


// do something with read data

Writing to a file