File IO: Difference between revisions
m (native initializers note) |
mNo edit summary |
||
Line 1: | Line 1: | ||
=Introduction= | |||
This article describes local file input/output in Javascript. | This article describes local file input/output in Javascript. | ||
Line 8: | Line 8: | ||
MonkeeSage's module is much smaller and very easy to use (simple examples are included in the 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)== | |||
<pre>var file = Components.classes["@mozilla.org/file/local;1"]. | <pre>var file = Components.classes["@mozilla.org/file/local;1"]. | ||
createInstance(Components.interfaces.nsILocalFile); | createInstance(Components.interfaces.nsILocalFile); | ||
Line 17: | Line 17: | ||
Note: the path passed to ''initWithPath()'' should be in 'native' form (eg. <tt>"C:\\Windows"</tt>). If you need to use ''file://'' URIs as initializers, see below. | Note: the path passed to ''initWithPath()'' should be in 'native' form (eg. <tt>"C:\\Windows"</tt>). If you need to use ''file://'' URIs as initializers, see below. | ||
==Opening special files== | |||
<pre>// get profile directory | <pre>// get profile directory | ||
var file = Components.classes["@mozilla.org/file/directory_service;1"]. | var file = Components.classes["@mozilla.org/file/directory_service;1"]. | ||
Line 57: | Line 57: | ||
==nsIFile and path strings== | |||
You can use nsIFile::path to get platform-specific path string, eg. <tt>C:\Windows\System32</tt> or <tt>/usr/share</tt>. If you want to get a ''file://'' URL of a file or an nsIFile from ''file://'' URL, you need to use [http://xulplanet.com/references/xpcomref/ifaces/nsIFileProtocolHandler.html nsIFileProtocolHandler]: | You can use nsIFile::path to get platform-specific path string, eg. <tt>C:\Windows\System32</tt> or <tt>/usr/share</tt>. If you want to get a ''file://'' URL of a file or an nsIFile from ''file://'' URL, you need to use [http://xulplanet.com/references/xpcomref/ifaces/nsIFileProtocolHandler.html nsIFileProtocolHandler]: | ||
<pre>// file is nsIFile | <pre>// file is nsIFile | ||
Line 69: | Line 69: | ||
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. | 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: | This snippet shows the right way to save a file path in user preferences: | ||
<pre>// |file| is nsILocalFile | <pre>// |file| is nsILocalFile | ||
Line 83: | Line 83: | ||
==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: <tt>file.append("myfile.txt");</tt> - to make ''file'' point to myfile.txt inside that directory. | Assume, ''file'' is an nsIFile pointing to some directory (f.e. a user profile directory). You can use this code: <tt>file.append("myfile.txt");</tt> - to make ''file'' point to myfile.txt inside that directory. | ||
Line 92: | Line 92: | ||
is possible, but the nsIFile::append() is much easier to read and is guaranteed to work on all platforms Mozilla itself works. | is possible, but the nsIFile::append() is much easier to read and is guaranteed to work on all platforms Mozilla itself works. | ||
===Traverse directories=== | |||
''todo'' | ''todo'' | ||
==Reading from a file== | |||
===Simple === | |||
===Line by line=== | |||
<pre> | <pre> | ||
// open an input stream from file | // open an input stream from file |
Revision as of 21:03, 16 September 2004
Introduction
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 XulPlanet.com.
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["@mozilla.org/file/local;1"]. createInstance(Components.interfaces.nsILocalFile); file.initWithPath("/home");
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.
Opening special files
// get profile directory var file = Components.classes["@mozilla.org/file/directory_service;1"]. createInstance(Components.interfaces.nsIProperties). get("ProfD", Components.interfaces.nsIFile);
Here is the list of 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["@mozilla.org/network/protocol;1?name=file"]. createInstance(Components.interfaces.nsIFileProtocolHandler). getURLSpecFromFile(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["@mozilla.org/preferences-service;1"]. getService(Components.interfaces.nsIPrefService). getBranch("extensions.myext."); prefs.setComplexValue("filename", Components.interfaces.nsILocalFile, file); /* You can read it later with this statement: var file = prefs.getComplexValue("filename", Components.interfaces.nsILocalFile); */
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 ((path.search(/\\/) != -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.
Traverse directories
todo
Reading from a file
Simple
Line by line
// open an input stream from file var istream = Components.classes["@mozilla.org/network/file-input-stream;1"] .createInstance(Components.interfaces.nsIFileInputStream); istream.init( file, 0x01, 00004, null ); istream.QueryInterface(Components.interfaces.nsILineInputStream); // read lines into array var line = {}, lines = {}, hasmore; do { hasmore = istream.readLine(line); lines.push(line.value); } while(hasmore); istream.close(); // do something with read data alert(lines);