5
<title>File System Node.js v0.6.18 Manual & Documentation</title>
6
<link rel="stylesheet" href="assets/style.css">
7
<link rel="stylesheet" href="assets/sh.css">
8
<link rel="canonical" href="http://nodejs.org/api/fs.html">
10
<body class="alt apidoc" id="api-section-fs">
11
<div id="intro" class="interior">
12
<a href="/" title="Go back to the home page">
13
<img id="logo" src="http://nodejs.org/images/logo-light.png" alt="node.js">
16
<div id="content" class="clearfix">
17
<div id="column2" class="interior">
19
<li><a href="/" class="home">Home</a></li>
20
<li><a href="/#download" class="download">Download</a></li>
21
<li><a href="/about/" class="about">About</a></li>
22
<li><a href="http://search.npmjs.org/" class="npm">npm Registry</a></li>
23
<li><a href="http://nodejs.org/api/" class="docs current">Docs</a></li>
24
<li><a href="http://blog.nodejs.org" class="blog">Blog</a></li>
25
<li><a href="/community/" class="community">Community</a></li>
26
<li><a href="/logos/" class="logos">Logos</a></li>
27
<li><a href="http://jobs.nodejs.org/" class="jobs">Jobs</a></li>
29
<p class="twitter"><a href="http://twitter.com/nodejs">@nodejs</a></p>
32
<div id="column1" class="interior">
34
<h1>Node.js v0.6.18 Manual & Documentation</h1>
37
<a href="index.html" name="toc">Index</a> |
38
<a href="all.html">View on single page</a> |
39
<a href="fs.json">View as JSON</a>
46
<h2>Table of Contents</h2>
48
<li><a href="#fs_file_system">File System</a><ul>
49
<li><a href="#fs_fs_rename_path1_path2_callback">fs.rename(path1, path2, [callback])</a></li>
50
<li><a href="#fs_fs_renamesync_path1_path2">fs.renameSync(path1, path2)</a></li>
51
<li><a href="#fs_fs_truncate_fd_len_callback">fs.truncate(fd, len, [callback])</a></li>
52
<li><a href="#fs_fs_truncatesync_fd_len">fs.truncateSync(fd, len)</a></li>
53
<li><a href="#fs_fs_chown_path_uid_gid_callback">fs.chown(path, uid, gid, [callback])</a></li>
54
<li><a href="#fs_fs_chownsync_path_uid_gid">fs.chownSync(path, uid, gid)</a></li>
55
<li><a href="#fs_fs_fchown_fd_uid_gid_callback">fs.fchown(fd, uid, gid, [callback])</a></li>
56
<li><a href="#fs_fs_fchownsync_fd_uid_gid">fs.fchownSync(fd, uid, gid)</a></li>
57
<li><a href="#fs_fs_lchown_path_uid_gid_callback">fs.lchown(path, uid, gid, [callback])</a></li>
58
<li><a href="#fs_fs_lchownsync_path_uid_gid">fs.lchownSync(path, uid, gid)</a></li>
59
<li><a href="#fs_fs_chmod_path_mode_callback">fs.chmod(path, mode, [callback])</a></li>
60
<li><a href="#fs_fs_chmodsync_path_mode">fs.chmodSync(path, mode)</a></li>
61
<li><a href="#fs_fs_fchmod_fd_mode_callback">fs.fchmod(fd, mode, [callback])</a></li>
62
<li><a href="#fs_fs_fchmodsync_fd_mode">fs.fchmodSync(fd, mode)</a></li>
63
<li><a href="#fs_fs_lchmod_path_mode_callback">fs.lchmod(path, mode, [callback])</a></li>
64
<li><a href="#fs_fs_lchmodsync_path_mode">fs.lchmodSync(path, mode)</a></li>
65
<li><a href="#fs_fs_stat_path_callback">fs.stat(path, [callback])</a></li>
66
<li><a href="#fs_fs_lstat_path_callback">fs.lstat(path, [callback])</a></li>
67
<li><a href="#fs_fs_fstat_fd_callback">fs.fstat(fd, [callback])</a></li>
68
<li><a href="#fs_fs_statsync_path">fs.statSync(path)</a></li>
69
<li><a href="#fs_fs_lstatsync_path">fs.lstatSync(path)</a></li>
70
<li><a href="#fs_fs_fstatsync_fd">fs.fstatSync(fd)</a></li>
71
<li><a href="#fs_fs_link_srcpath_dstpath_callback">fs.link(srcpath, dstpath, [callback])</a></li>
72
<li><a href="#fs_fs_linksync_srcpath_dstpath">fs.linkSync(srcpath, dstpath)</a></li>
73
<li><a href="#fs_fs_symlink_linkdata_path_type_callback">fs.symlink(linkdata, path, [type], [callback])</a></li>
74
<li><a href="#fs_fs_symlinksync_linkdata_path_type">fs.symlinkSync(linkdata, path, [type])</a></li>
75
<li><a href="#fs_fs_readlink_path_callback">fs.readlink(path, [callback])</a></li>
76
<li><a href="#fs_fs_readlinksync_path">fs.readlinkSync(path)</a></li>
77
<li><a href="#fs_fs_realpath_path_callback">fs.realpath(path, [callback])</a></li>
78
<li><a href="#fs_fs_realpathsync_path">fs.realpathSync(path)</a></li>
79
<li><a href="#fs_fs_unlink_path_callback">fs.unlink(path, [callback])</a></li>
80
<li><a href="#fs_fs_unlinksync_path">fs.unlinkSync(path)</a></li>
81
<li><a href="#fs_fs_rmdir_path_callback">fs.rmdir(path, [callback])</a></li>
82
<li><a href="#fs_fs_rmdirsync_path">fs.rmdirSync(path)</a></li>
83
<li><a href="#fs_fs_mkdir_path_mode_callback">fs.mkdir(path, [mode], [callback])</a></li>
84
<li><a href="#fs_fs_mkdirsync_path_mode">fs.mkdirSync(path, [mode])</a></li>
85
<li><a href="#fs_fs_readdir_path_callback">fs.readdir(path, [callback])</a></li>
86
<li><a href="#fs_fs_readdirsync_path">fs.readdirSync(path)</a></li>
87
<li><a href="#fs_fs_close_fd_callback">fs.close(fd, [callback])</a></li>
88
<li><a href="#fs_fs_closesync_fd">fs.closeSync(fd)</a></li>
89
<li><a href="#fs_fs_open_path_flags_mode_callback">fs.open(path, flags, [mode], [callback])</a></li>
90
<li><a href="#fs_fs_opensync_path_flags_mode">fs.openSync(path, flags, [mode])</a></li>
91
<li><a href="#fs_fs_utimes_path_atime_mtime_callback">fs.utimes(path, atime, mtime, [callback])</a></li>
92
<li><a href="#fs_fs_utimessync_path_atime_mtime">fs.utimesSync(path, atime, mtime)</a></li>
93
<li><a href="#fs_fs_futimes_fd_atime_mtime_callback">fs.futimes(fd, atime, mtime, [callback])</a></li>
94
<li><a href="#fs_fs_futimessync_fd_atime_mtime">fs.futimesSync(fd, atime, mtime)</a></li>
95
<li><a href="#fs_fs_fsync_fd_callback">fs.fsync(fd, [callback])</a></li>
96
<li><a href="#fs_fs_fsyncsync_fd">fs.fsyncSync(fd)</a></li>
97
<li><a href="#fs_fs_write_fd_buffer_offset_length_position_callback">fs.write(fd, buffer, offset, length, position, [callback])</a></li>
98
<li><a href="#fs_fs_writesync_fd_buffer_offset_length_position">fs.writeSync(fd, buffer, offset, length, position)</a></li>
99
<li><a href="#fs_fs_writesync_fd_str_position_encoding">fs.writeSync(fd, str, position, [encoding])</a></li>
100
<li><a href="#fs_fs_read_fd_buffer_offset_length_position_callback">fs.read(fd, buffer, offset, length, position, [callback])</a></li>
101
<li><a href="#fs_fs_readsync_fd_buffer_offset_length_position">fs.readSync(fd, buffer, offset, length, position)</a></li>
102
<li><a href="#fs_fs_readsync_fd_length_position_encoding">fs.readSync(fd, length, position, encoding)</a></li>
103
<li><a href="#fs_fs_readfile_filename_encoding_callback">fs.readFile(filename, [encoding], [callback])</a></li>
104
<li><a href="#fs_fs_readfilesync_filename_encoding">fs.readFileSync(filename, [encoding])</a></li>
105
<li><a href="#fs_fs_writefile_filename_data_encoding_callback">fs.writeFile(filename, data, [encoding], [callback])</a></li>
106
<li><a href="#fs_fs_writefilesync_filename_data_encoding">fs.writeFileSync(filename, data, [encoding])</a></li>
107
<li><a href="#fs_fs_watchfile_filename_options_listener">fs.watchFile(filename, [options], listener)</a></li>
108
<li><a href="#fs_fs_unwatchfile_filename">fs.unwatchFile(filename)</a></li>
109
<li><a href="#fs_fs_watch_filename_options_listener">fs.watch(filename, [options], listener)</a><ul>
110
<li><a href="#fs_caveats">Caveats</a><ul>
111
<li><a href="#fs_availability">Availability</a></li>
112
<li><a href="#fs_filename_argument">Filename Argument</a></li>
117
<li><a href="#fs_class_fs_stats">Class: fs.Stats</a></li>
118
<li><a href="#fs_fs_createreadstream_path_options">fs.createReadStream(path, [options])</a></li>
119
<li><a href="#fs_class_fs_readstream">Class: fs.ReadStream</a><ul>
120
<li><a href="#fs_event_open">Event: 'open'</a></li>
123
<li><a href="#fs_fs_createwritestream_path_options">fs.createWriteStream(path, [options])</a></li>
124
<li><a href="#fs_fs_writestream">fs.WriteStream</a><ul>
125
<li><a href="#fs_event_open_1">Event: 'open'</a></li>
126
<li><a href="#fs_file_byteswritten">file.bytesWritten</a></li>
129
<li><a href="#fs_class_fs_fswatcher">Class: fs.FSWatcher</a><ul>
130
<li><a href="#fs_watcher_close">watcher.close()</a></li>
131
<li><a href="#fs_event_change">Event: 'change'</a></li>
132
<li><a href="#fs_event_error">Event: 'error'</a></li>
141
<div id="apicontent">
142
<h1>File System<span><a class="mark" href="#fs_file_system" id="fs_file_system">#</a></span></h1>
143
<pre><code>Stability: 3 - Stable</code></pre>
146
<p>File I/O is provided by simple wrappers around standard POSIX functions. To
147
use this module do <code>require('fs')</code>. All the methods have asynchronous and
151
<p>The asynchronous form always take a completion callback as its last argument.
152
The arguments passed to the completion callback depend on the method, but the
153
first argument is always reserved for an exception. If the operation was
154
completed successfully, then the first argument will be <code>null</code> or <code>undefined</code>.
157
<p>When using the synchronous form any exceptions are immediately thrown.
158
You can use try/catch to handle exceptions or allow them to bubble up.
161
<p>Here is an example of the asynchronous version:
164
<pre><code>var fs = require('fs');
166
fs.unlink('/tmp/hello', function (err) {
168
console.log('successfully deleted /tmp/hello');
170
<p>Here is the synchronous version:
173
<pre><code>var fs = require('fs');
175
fs.unlinkSync('/tmp/hello')
176
console.log('successfully deleted /tmp/hello');</code></pre>
177
<p>With the asynchronous methods there is no guaranteed ordering. So the
178
following is prone to error:
181
<pre><code>fs.rename('/tmp/hello', '/tmp/world', function (err) {
183
console.log('renamed complete');
185
fs.stat('/tmp/world', function (err, stats) {
187
console.log('stats: ' + JSON.stringify(stats));
189
<p>It could be that <code>fs.stat</code> is executed before <code>fs.rename</code>.
190
The correct way to do this is to chain the callbacks.
193
<pre><code>fs.rename('/tmp/hello', '/tmp/world', function (err) {
195
fs.stat('/tmp/world', function (err, stats) {
197
console.log('stats: ' + JSON.stringify(stats));
200
<p>In busy processes, the programmer is <em>strongly encouraged</em> to use the
201
asynchronous versions of these calls. The synchronous versions will block
202
the entire process until they complete--halting all connections.
205
<p>Relative path to filename can be used, remember however that this path will be relative
206
to <code>process.cwd()</code>.
209
<h2>fs.rename(path1, path2, [callback])<span><a class="mark" href="#fs_fs_rename_path1_path2_callback" id="fs_fs_rename_path1_path2_callback">#</a></span></h2>
210
<p>Asynchronous rename(2). No arguments other than a possible exception are given
211
to the completion callback.
214
<h2>fs.renameSync(path1, path2)<span><a class="mark" href="#fs_fs_renamesync_path1_path2" id="fs_fs_renamesync_path1_path2">#</a></span></h2>
215
<p>Synchronous rename(2).
218
<h2>fs.truncate(fd, len, [callback])<span><a class="mark" href="#fs_fs_truncate_fd_len_callback" id="fs_fs_truncate_fd_len_callback">#</a></span></h2>
219
<p>Asynchronous ftruncate(2). No arguments other than a possible exception are
220
given to the completion callback.
223
<h2>fs.truncateSync(fd, len)<span><a class="mark" href="#fs_fs_truncatesync_fd_len" id="fs_fs_truncatesync_fd_len">#</a></span></h2>
224
<p>Synchronous ftruncate(2).
227
<h2>fs.chown(path, uid, gid, [callback])<span><a class="mark" href="#fs_fs_chown_path_uid_gid_callback" id="fs_fs_chown_path_uid_gid_callback">#</a></span></h2>
228
<p>Asynchronous chown(2). No arguments other than a possible exception are given
229
to the completion callback.
232
<h2>fs.chownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_chownsync_path_uid_gid" id="fs_fs_chownsync_path_uid_gid">#</a></span></h2>
233
<p>Synchronous chown(2).
236
<h2>fs.fchown(fd, uid, gid, [callback])<span><a class="mark" href="#fs_fs_fchown_fd_uid_gid_callback" id="fs_fs_fchown_fd_uid_gid_callback">#</a></span></h2>
237
<p>Asynchronous fchown(2). No arguments other than a possible exception are given
238
to the completion callback.
241
<h2>fs.fchownSync(fd, uid, gid)<span><a class="mark" href="#fs_fs_fchownsync_fd_uid_gid" id="fs_fs_fchownsync_fd_uid_gid">#</a></span></h2>
242
<p>Synchronous fchown(2).
245
<h2>fs.lchown(path, uid, gid, [callback])<span><a class="mark" href="#fs_fs_lchown_path_uid_gid_callback" id="fs_fs_lchown_path_uid_gid_callback">#</a></span></h2>
246
<p>Asynchronous lchown(2). No arguments other than a possible exception are given
247
to the completion callback.
250
<h2>fs.lchownSync(path, uid, gid)<span><a class="mark" href="#fs_fs_lchownsync_path_uid_gid" id="fs_fs_lchownsync_path_uid_gid">#</a></span></h2>
251
<p>Synchronous lchown(2).
254
<h2>fs.chmod(path, mode, [callback])<span><a class="mark" href="#fs_fs_chmod_path_mode_callback" id="fs_fs_chmod_path_mode_callback">#</a></span></h2>
255
<p>Asynchronous chmod(2). No arguments other than a possible exception are given
256
to the completion callback.
259
<h2>fs.chmodSync(path, mode)<span><a class="mark" href="#fs_fs_chmodsync_path_mode" id="fs_fs_chmodsync_path_mode">#</a></span></h2>
260
<p>Synchronous chmod(2).
263
<h2>fs.fchmod(fd, mode, [callback])<span><a class="mark" href="#fs_fs_fchmod_fd_mode_callback" id="fs_fs_fchmod_fd_mode_callback">#</a></span></h2>
264
<p>Asynchronous fchmod(2). No arguments other than a possible exception
265
are given to the completion callback.
268
<h2>fs.fchmodSync(fd, mode)<span><a class="mark" href="#fs_fs_fchmodsync_fd_mode" id="fs_fs_fchmodsync_fd_mode">#</a></span></h2>
269
<p>Synchronous fchmod(2).
272
<h2>fs.lchmod(path, mode, [callback])<span><a class="mark" href="#fs_fs_lchmod_path_mode_callback" id="fs_fs_lchmod_path_mode_callback">#</a></span></h2>
273
<p>Asynchronous lchmod(2). No arguments other than a possible exception
274
are given to the completion callback.
277
<h2>fs.lchmodSync(path, mode)<span><a class="mark" href="#fs_fs_lchmodsync_path_mode" id="fs_fs_lchmodsync_path_mode">#</a></span></h2>
278
<p>Synchronous lchmod(2).
281
<h2>fs.stat(path, [callback])<span><a class="mark" href="#fs_fs_stat_path_callback" id="fs_fs_stat_path_callback">#</a></span></h2>
282
<p>Asynchronous stat(2). The callback gets two arguments <code>(err, stats)</code> where
283
<code>stats</code> is a <a href="#fs_class_fs_stats">fs.Stats</a> object. See the <a href="#fs_class_fs_stats">fs.Stats</a>
284
section below for more information.
287
<h2>fs.lstat(path, [callback])<span><a class="mark" href="#fs_fs_lstat_path_callback" id="fs_fs_lstat_path_callback">#</a></span></h2>
288
<p>Asynchronous lstat(2). The callback gets two arguments <code>(err, stats)</code> where
289
<code>stats</code> is a <code>fs.Stats</code> object. <code>lstat()</code> is identical to <code>stat()</code>, except that if
290
<code>path</code> is a symbolic link, then the link itself is stat-ed, not the file that it
294
<h2>fs.fstat(fd, [callback])<span><a class="mark" href="#fs_fs_fstat_fd_callback" id="fs_fs_fstat_fd_callback">#</a></span></h2>
295
<p>Asynchronous fstat(2). The callback gets two arguments <code>(err, stats)</code> where
296
<code>stats</code> is a <code>fs.Stats</code> object. <code>fstat()</code> is identical to <code>stat()</code>, except that
297
the file to be stat-ed is specified by the file descriptor <code>fd</code>.
300
<h2>fs.statSync(path)<span><a class="mark" href="#fs_fs_statsync_path" id="fs_fs_statsync_path">#</a></span></h2>
301
<p>Synchronous stat(2). Returns an instance of <code>fs.Stats</code>.
304
<h2>fs.lstatSync(path)<span><a class="mark" href="#fs_fs_lstatsync_path" id="fs_fs_lstatsync_path">#</a></span></h2>
305
<p>Synchronous lstat(2). Returns an instance of <code>fs.Stats</code>.
308
<h2>fs.fstatSync(fd)<span><a class="mark" href="#fs_fs_fstatsync_fd" id="fs_fs_fstatsync_fd">#</a></span></h2>
309
<p>Synchronous fstat(2). Returns an instance of <code>fs.Stats</code>.
312
<h2>fs.link(srcpath, dstpath, [callback])<span><a class="mark" href="#fs_fs_link_srcpath_dstpath_callback" id="fs_fs_link_srcpath_dstpath_callback">#</a></span></h2>
313
<p>Asynchronous link(2). No arguments other than a possible exception are given to
314
the completion callback.
317
<h2>fs.linkSync(srcpath, dstpath)<span><a class="mark" href="#fs_fs_linksync_srcpath_dstpath" id="fs_fs_linksync_srcpath_dstpath">#</a></span></h2>
318
<p>Synchronous link(2).
321
<h2>fs.symlink(linkdata, path, [type], [callback])<span><a class="mark" href="#fs_fs_symlink_linkdata_path_type_callback" id="fs_fs_symlink_linkdata_path_type_callback">#</a></span></h2>
322
<p>Asynchronous symlink(2). No arguments other than a possible exception are given
323
to the completion callback.
324
<code>type</code> argument can be either <code>'dir'</code> or <code>'file'</code> (default is <code>'file'</code>). It is only
325
used on Windows (ignored on other platforms).
328
<h2>fs.symlinkSync(linkdata, path, [type])<span><a class="mark" href="#fs_fs_symlinksync_linkdata_path_type" id="fs_fs_symlinksync_linkdata_path_type">#</a></span></h2>
329
<p>Synchronous symlink(2).
332
<h2>fs.readlink(path, [callback])<span><a class="mark" href="#fs_fs_readlink_path_callback" id="fs_fs_readlink_path_callback">#</a></span></h2>
333
<p>Asynchronous readlink(2). The callback gets two arguments <code>(err,
337
<h2>fs.readlinkSync(path)<span><a class="mark" href="#fs_fs_readlinksync_path" id="fs_fs_readlinksync_path">#</a></span></h2>
338
<p>Synchronous readlink(2). Returns the symbolic link's string value.
341
<h2>fs.realpath(path, [callback])<span><a class="mark" href="#fs_fs_realpath_path_callback" id="fs_fs_realpath_path_callback">#</a></span></h2>
342
<p>Asynchronous realpath(2). The callback gets two arguments <code>(err,
343
resolvedPath)</code>. May use <code>process.cwd</code> to resolve relative paths.
346
<h2>fs.realpathSync(path)<span><a class="mark" href="#fs_fs_realpathsync_path" id="fs_fs_realpathsync_path">#</a></span></h2>
347
<p>Synchronous realpath(2). Returns the resolved path.
350
<h2>fs.unlink(path, [callback])<span><a class="mark" href="#fs_fs_unlink_path_callback" id="fs_fs_unlink_path_callback">#</a></span></h2>
351
<p>Asynchronous unlink(2). No arguments other than a possible exception are given
352
to the completion callback.
355
<h2>fs.unlinkSync(path)<span><a class="mark" href="#fs_fs_unlinksync_path" id="fs_fs_unlinksync_path">#</a></span></h2>
356
<p>Synchronous unlink(2).
359
<h2>fs.rmdir(path, [callback])<span><a class="mark" href="#fs_fs_rmdir_path_callback" id="fs_fs_rmdir_path_callback">#</a></span></h2>
360
<p>Asynchronous rmdir(2). No arguments other than a possible exception are given
361
to the completion callback.
364
<h2>fs.rmdirSync(path)<span><a class="mark" href="#fs_fs_rmdirsync_path" id="fs_fs_rmdirsync_path">#</a></span></h2>
365
<p>Synchronous rmdir(2).
368
<h2>fs.mkdir(path, [mode], [callback])<span><a class="mark" href="#fs_fs_mkdir_path_mode_callback" id="fs_fs_mkdir_path_mode_callback">#</a></span></h2>
369
<p>Asynchronous mkdir(2). No arguments other than a possible exception are given
370
to the completion callback. <code>mode</code> defaults to <code>0777</code>.
373
<h2>fs.mkdirSync(path, [mode])<span><a class="mark" href="#fs_fs_mkdirsync_path_mode" id="fs_fs_mkdirsync_path_mode">#</a></span></h2>
374
<p>Synchronous mkdir(2).
377
<h2>fs.readdir(path, [callback])<span><a class="mark" href="#fs_fs_readdir_path_callback" id="fs_fs_readdir_path_callback">#</a></span></h2>
378
<p>Asynchronous readdir(3). Reads the contents of a directory.
379
The callback gets two arguments <code>(err, files)</code> where <code>files</code> is an array of
380
the names of the files in the directory excluding <code>'.'</code> and <code>'..'</code>.
383
<h2>fs.readdirSync(path)<span><a class="mark" href="#fs_fs_readdirsync_path" id="fs_fs_readdirsync_path">#</a></span></h2>
384
<p>Synchronous readdir(3). Returns an array of filenames excluding <code>'.'</code> and
385
<code>'..'</code>.
388
<h2>fs.close(fd, [callback])<span><a class="mark" href="#fs_fs_close_fd_callback" id="fs_fs_close_fd_callback">#</a></span></h2>
389
<p>Asynchronous close(2). No arguments other than a possible exception are given
390
to the completion callback.
393
<h2>fs.closeSync(fd)<span><a class="mark" href="#fs_fs_closesync_fd" id="fs_fs_closesync_fd">#</a></span></h2>
394
<p>Synchronous close(2).
397
<h2>fs.open(path, flags, [mode], [callback])<span><a class="mark" href="#fs_fs_open_path_flags_mode_callback" id="fs_fs_open_path_flags_mode_callback">#</a></span></h2>
398
<p>Asynchronous file open. See open(2). <code>flags</code> can be:
402
<li><p><code>'r'</code> - Open file for reading.
403
An exception occurs if the file does not exist.</p>
405
<li><p><code>'r+'</code> - Open file for reading and writing.
406
An exception occurs if the file does not exist.</p>
408
<li><p><code>'w'</code> - Open file for writing.
409
The file is created (if it does not exist) or truncated (if it exists).</p>
411
<li><p><code>'w+'</code> - Open file for reading and writing.
412
The file is created (if it does not exist) or truncated (if it exists).</p>
414
<li><p><code>'a'</code> - Open file for appending.
415
The file is created if it does not exist.</p>
417
<li><p><code>'a+'</code> - Open file for reading and appending.
418
The file is created if it does not exist.</p>
421
<p><code>mode</code> defaults to <code>0666</code>. The callback gets two arguments <code>(err, fd)</code>.
424
<h2>fs.openSync(path, flags, [mode])<span><a class="mark" href="#fs_fs_opensync_path_flags_mode" id="fs_fs_opensync_path_flags_mode">#</a></span></h2>
425
<p>Synchronous open(2).
428
<h2>fs.utimes(path, atime, mtime, [callback])<span><a class="mark" href="#fs_fs_utimes_path_atime_mtime_callback" id="fs_fs_utimes_path_atime_mtime_callback">#</a></span></h2>
429
<h2>fs.utimesSync(path, atime, mtime)<span><a class="mark" href="#fs_fs_utimessync_path_atime_mtime" id="fs_fs_utimessync_path_atime_mtime">#</a></span></h2>
430
<p>Change file timestamps of the file referenced by the supplied path.
433
<h2>fs.futimes(fd, atime, mtime, [callback])<span><a class="mark" href="#fs_fs_futimes_fd_atime_mtime_callback" id="fs_fs_futimes_fd_atime_mtime_callback">#</a></span></h2>
434
<h2>fs.futimesSync(fd, atime, mtime)<span><a class="mark" href="#fs_fs_futimessync_fd_atime_mtime" id="fs_fs_futimessync_fd_atime_mtime">#</a></span></h2>
435
<p>Change the file timestamps of a file referenced by the supplied file
439
<h2>fs.fsync(fd, [callback])<span><a class="mark" href="#fs_fs_fsync_fd_callback" id="fs_fs_fsync_fd_callback">#</a></span></h2>
440
<p>Asynchronous fsync(2). No arguments other than a possible exception are given
441
to the completion callback.
444
<h2>fs.fsyncSync(fd)<span><a class="mark" href="#fs_fs_fsyncsync_fd" id="fs_fs_fsyncsync_fd">#</a></span></h2>
445
<p>Synchronous fsync(2).
448
<h2>fs.write(fd, buffer, offset, length, position, [callback])<span><a class="mark" href="#fs_fs_write_fd_buffer_offset_length_position_callback" id="fs_fs_write_fd_buffer_offset_length_position_callback">#</a></span></h2>
449
<p>Write <code>buffer</code> to the file specified by <code>fd</code>.
452
<p><code>offset</code> and <code>length</code> determine the part of the buffer to be written.
455
<p><code>position</code> refers to the offset from the beginning of the file where this data
456
should be written. If <code>position</code> is <code>null</code>, the data will be written at the
461
<p>The callback will be given three arguments <code>(err, written, buffer)</code> where <code>written</code>
462
specifies how many <em>bytes</em> were written from <code>buffer</code>.
465
<p>Note that it is unsafe to use <code>fs.write</code> multiple times on the same file
466
without waiting for the callback. For this scenario,
467
<code>fs.createWriteStream</code> is strongly recommended.
470
<h2>fs.writeSync(fd, buffer, offset, length, position)<span><a class="mark" href="#fs_fs_writesync_fd_buffer_offset_length_position" id="fs_fs_writesync_fd_buffer_offset_length_position">#</a></span></h2>
471
<p>Synchronous version of buffer-based <code>fs.write()</code>. Returns the number of bytes
475
<h2>fs.writeSync(fd, str, position, [encoding])<span><a class="mark" href="#fs_fs_writesync_fd_str_position_encoding" id="fs_fs_writesync_fd_str_position_encoding">#</a></span></h2>
476
<p>Synchronous version of string-based <code>fs.write()</code>. <code>encoding</code> defaults to
477
<code>'utf8'</code>. Returns the number of <em>bytes</em> written.
480
<h2>fs.read(fd, buffer, offset, length, position, [callback])<span><a class="mark" href="#fs_fs_read_fd_buffer_offset_length_position_callback" id="fs_fs_read_fd_buffer_offset_length_position_callback">#</a></span></h2>
481
<p>Read data from the file specified by <code>fd</code>.
484
<p><code>buffer</code> is the buffer that the data will be written to.
487
<p><code>offset</code> is offset within the buffer where writing will start.
490
<p><code>length</code> is an integer specifying the number of bytes to read.
493
<p><code>position</code> is an integer specifying where to begin reading from in the file.
494
If <code>position</code> is <code>null</code>, data will be read from the current file position.
497
<p>The callback is given the three arguments, <code>(err, bytesRead, buffer)</code>.
500
<h2>fs.readSync(fd, buffer, offset, length, position)<span><a class="mark" href="#fs_fs_readsync_fd_buffer_offset_length_position" id="fs_fs_readsync_fd_buffer_offset_length_position">#</a></span></h2>
501
<p>Synchronous version of buffer-based <code>fs.read</code>. Returns the number of
502
<code>bytesRead</code>.
505
<h2>fs.readSync(fd, length, position, encoding)<span><a class="mark" href="#fs_fs_readsync_fd_length_position_encoding" id="fs_fs_readsync_fd_length_position_encoding">#</a></span></h2>
506
<p>Synchronous version of string-based <code>fs.read</code>. Returns the number of
507
<code>bytesRead</code>.
510
<h2>fs.readFile(filename, [encoding], [callback])<span><a class="mark" href="#fs_fs_readfile_filename_encoding_callback" id="fs_fs_readfile_filename_encoding_callback">#</a></span></h2>
511
<p>Asynchronously reads the entire contents of a file. Example:
514
<pre><code>fs.readFile('/etc/passwd', function (err, data) {
518
<p>The callback is passed two arguments <code>(err, data)</code>, where <code>data</code> is the
519
contents of the file.
522
<p>If no encoding is specified, then the raw buffer is returned.
526
<h2>fs.readFileSync(filename, [encoding])<span><a class="mark" href="#fs_fs_readfilesync_filename_encoding" id="fs_fs_readfilesync_filename_encoding">#</a></span></h2>
527
<p>Synchronous version of <code>fs.readFile</code>. Returns the contents of the <code>filename</code>.
530
<p>If <code>encoding</code> is specified then this function returns a string. Otherwise it
535
<h2>fs.writeFile(filename, data, [encoding], [callback])<span><a class="mark" href="#fs_fs_writefile_filename_data_encoding_callback" id="fs_fs_writefile_filename_data_encoding_callback">#</a></span></h2>
536
<p>Asynchronously writes data to a file, replacing the file if it already exists.
537
<code>data</code> can be a string or a buffer. The <code>encoding</code> argument is ignored if
538
<code>data</code> is a buffer. It defaults to <code>'utf8'</code>.
544
<pre><code>fs.writeFile('message.txt', 'Hello Node', function (err) {
546
console.log('It\'s saved!');
548
<h2>fs.writeFileSync(filename, data, [encoding])<span><a class="mark" href="#fs_fs_writefilesync_filename_data_encoding" id="fs_fs_writefilesync_filename_data_encoding">#</a></span></h2>
549
<p>The synchronous version of <code>fs.writeFile</code>.
552
<h2>fs.watchFile(filename, [options], listener)<span><a class="mark" href="#fs_fs_watchfile_filename_options_listener" id="fs_fs_watchfile_filename_options_listener">#</a></span></h2>
553
<pre><code>Stability: 2 - Unstable. Use fs.watch instead, if available.</code></pre>
554
<p>Watch for changes on <code>filename</code>. The callback <code>listener</code> will be called each
555
time the file is accessed.
558
<p>The second argument is optional. The <code>options</code> if provided should be an object
559
containing two members a boolean, <code>persistent</code>, and <code>interval</code>. <code>persistent</code>
560
indicates whether the process should continue to run as long as files are
561
being watched. <code>interval</code> indicates how often the target should be polled,
562
in milliseconds. (On Linux systems with inotify, <code>interval</code> is ignored.) The
563
default is <code>{ persistent: true, interval: 0 }</code>.
566
<p>The <code>listener</code> gets two arguments the current stat object and the previous
570
<pre><code>fs.watchFile('message.text', function (curr, prev) {
571
console.log('the current mtime is: ' + curr.mtime);
572
console.log('the previous mtime was: ' + prev.mtime);
574
<p>These stat objects are instances of <code>fs.Stat</code>.
577
<p>If you want to be notified when the file was modified, not just accessed
578
you need to compare <code>curr.mtime</code> and <code>prev.mtime</code>.
582
<h2>fs.unwatchFile(filename)<span><a class="mark" href="#fs_fs_unwatchfile_filename" id="fs_fs_unwatchfile_filename">#</a></span></h2>
583
<pre><code>Stability: 2 - Unstable. Use fs.watch instead, if available.</code></pre>
584
<p>Stop watching for changes on <code>filename</code>.
587
<h2>fs.watch(filename, [options], listener)<span><a class="mark" href="#fs_fs_watch_filename_options_listener" id="fs_fs_watch_filename_options_listener">#</a></span></h2>
588
<pre><code>Stability: 2 - Unstable. Not available on all platforms.</code></pre>
589
<p>Watch for changes on <code>filename</code>, where <code>filename</code> is either a file or a
590
directory. The returned object is a <a href="#fs_class_fs_fswatcher">fs.FSWatcher</a>.
593
<p>The second argument is optional. The <code>options</code> if provided should be an object
594
containing a boolean member <code>persistent</code>, which indicates whether the process
595
should continue to run as long as files are being watched. The default is
596
<code>{ persistent: true }</code>.
599
<p>The listener callback gets two arguments <code>(event, filename)</code>. <code>event</code> is either
600
'rename' or 'change', and <code>filename</code> is the name of the file which triggered
604
<h3>Caveats<span><a class="mark" href="#fs_caveats" id="fs_caveats">#</a></span></h3>
607
<p>The <code>fs.watch</code> API is not 100% consistent across platforms, and is
608
unavailable in some situations.
611
<h4>Availability<span><a class="mark" href="#fs_availability" id="fs_availability">#</a></span></h4>
614
<p>This feature depends on the underlying operating system providing a way
615
to be notified of filesystem changes.
619
<li>On Linux systems, this uses <code>inotify</code>.</li>
620
<li>On BSD systems (including OS X), this uses <code>kqueue</code>.</li>
621
<li>On SunOS systems (including Solaris and SmartOS), this uses <code>event ports</code>.</li>
622
<li>On Windows systems, this feature depends on <code>ReadDirectoryChangesW</code>.</li>
624
<p>If the underlying functionality is not available for some reason, then
625
<code>fs.watch</code> will not be able to function. You can still use
626
<code>fs.watchFile</code>, which uses stat polling, but it is slower and less
630
<h4>Filename Argument<span><a class="mark" href="#fs_filename_argument" id="fs_filename_argument">#</a></span></h4>
633
<p>Providing <code>filename</code> argument in the callback is not supported
634
on every platform (currently it's only supported on Linux and Windows). Even
635
on supported platforms <code>filename</code> is not always guaranteed to be provided.
636
Therefore, don't assume that <code>filename</code> argument is always provided in the
637
callback, and have some fallback logic if it is null.
640
<pre><code>fs.watch('somedir', function (event, filename) {
641
console.log('event is: ' + event);
643
console.log('filename provided: ' + filename);
645
console.log('filename not provided');
648
<h2>Class: fs.Stats<span><a class="mark" href="#fs_class_fs_stats" id="fs_class_fs_stats">#</a></span></h2>
649
<p>Objects returned from <code>fs.stat()</code>, <code>fs.lstat()</code> and <code>fs.fstat()</code> and their
650
synchronous counterparts are of this type.
654
<li><code>stats.isFile()</code></li>
655
<li><code>stats.isDirectory()</code></li>
656
<li><code>stats.isBlockDevice()</code></li>
657
<li><code>stats.isCharacterDevice()</code></li>
658
<li><code>stats.isSymbolicLink()</code> (only valid with <code>fs.lstat()</code>)</li>
659
<li><code>stats.isFIFO()</code></li>
660
<li><code>stats.isSocket()</code></li>
662
<p>For a regular file <code>util.inspect(stats)</code> would return a string very
666
<pre><code>{ dev: 2114,
676
atime: Mon, 10 Oct 2011 23:24:11 GMT,
677
mtime: Mon, 10 Oct 2011 23:24:11 GMT,
678
ctime: Mon, 10 Oct 2011 23:24:11 GMT }</code></pre>
679
<p>Please note that <code>atime</code>, <code>mtime</code> and <code>ctime</code> are instances
680
of <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date">Date</a> object and to compare the values of
681
these objects you should use appropriate methods. For most
682
general uses <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime">getTime()</a> will return
683
the number of milliseconds elapsed since <em>1 January 1970
684
00:00:00 UTC</em> and this integer should be sufficient for
685
any comparison, however there additional methods which can
686
be used for displaying fuzzy information. More details can
687
be found in the <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date">MDN JavaScript Reference</a> page.
690
<h2>fs.createReadStream(path, [options])<span><a class="mark" href="#fs_fs_createreadstream_path_options" id="fs_fs_createreadstream_path_options">#</a></span></h2>
691
<p>Returns a new ReadStream object (See <code>Readable Stream</code>).
694
<p><code>options</code> is an object with the following defaults:
697
<pre><code>{ flags: 'r',
701
bufferSize: 64 * 1024
703
<p><code>options</code> can include <code>start</code> and <code>end</code> values to read a range of bytes from
704
the file instead of the entire file. Both <code>start</code> and <code>end</code> are inclusive and
708
<p>An example to read the last 10 bytes of a file which is 100 bytes long:
711
<pre><code>fs.createReadStream('sample.txt', {start: 90, end: 99});</code></pre>
712
<h2>Class: fs.ReadStream<span><a class="mark" href="#fs_class_fs_readstream" id="fs_class_fs_readstream">#</a></span></h2>
713
<p><code>ReadStream</code> is a <a href="stream.html#stream_readable_stream">Readable Stream</a>.
716
<h3>Event: 'open'<span><a class="mark" href="#fs_event_open" id="fs_event_open">#</a></span></h3>
717
<div class="signature"><ul>
718
<li><code>fd</code> <span class="type">Integer</span> file descriptor used by the ReadStream.</li>
720
<p>Emitted when the ReadStream's file is opened.
724
<h2>fs.createWriteStream(path, [options])<span><a class="mark" href="#fs_fs_createwritestream_path_options" id="fs_fs_createwritestream_path_options">#</a></span></h2>
725
<p>Returns a new WriteStream object (See <code>Writable Stream</code>).
728
<p><code>options</code> is an object with the following defaults:
731
<pre><code>{ flags: 'w',
733
mode: 0666 }</code></pre>
734
<p><code>options</code> may also include a <code>start</code> option to allow writing data at
735
some position past the beginning of the file. Modifying a file rather
736
than replacing it may require a <code>flags</code> mode of <code>r+</code> rather than the
737
default mode <code>w</code>.
740
<h2>fs.WriteStream<span><a class="mark" href="#fs_fs_writestream" id="fs_fs_writestream">#</a></span></h2>
741
<p><code>WriteStream</code> is a <a href="stream.html#stream_writable_stream">Writable Stream</a>.
744
<h3>Event: 'open'<span><a class="mark" href="#fs_event_open_1" id="fs_event_open_1">#</a></span></h3>
745
<div class="signature"><ul>
746
<li><code>fd</code> <span class="type">Integer</span> file descriptor used by the ReadStream.</li>
748
<p>Emitted when the WriteStream's file is opened.
751
<h3>file.bytesWritten<span><a class="mark" href="#fs_file_byteswritten" id="fs_file_byteswritten">#</a></span></h3>
752
<p>The number of bytes written so far. Does not include data that is still queued
756
<h2>Class: fs.FSWatcher<span><a class="mark" href="#fs_class_fs_fswatcher" id="fs_class_fs_fswatcher">#</a></span></h2>
757
<p>Objects returned from <code>fs.watch()</code> are of this type.
760
<h3>watcher.close()<span><a class="mark" href="#fs_watcher_close" id="fs_watcher_close">#</a></span></h3>
761
<p>Stop watching for changes on the given <code>fs.FSWatcher</code>.
764
<h3>Event: 'change'<span><a class="mark" href="#fs_event_change" id="fs_event_change">#</a></span></h3>
765
<div class="signature"><ul>
766
<li><code>event</code> <span class="type">String</span> The type of fs change</li>
767
<li><code>filename</code> <span class="type">String</span> The filename that changed (if relevant/available)</li>
769
<p>Emitted when something changes in a watched directory or file.
770
See more details in <a href="#fs_fs_watch_filename_options_listener">fs.watch</a>.
773
<h3>Event: 'error'<span><a class="mark" href="#fs_event_error" id="fs_event_error">#</a></span></h3>
774
<div class="signature"><ul>
775
<li><code>error</code> <span class="type">Error object</span></li>
777
<p>Emitted when an error occurs.
784
<ul class="clearfix">
785
<li><a href="/">Node.js</a></li>
786
<li><a href="/#download">Download</a></li>
787
<li><a href="/about/">About</a></li>
788
<li><a href="http://search.npmjs.org/">npm Registry</a></li>
789
<li><a href="http://nodejs.org/api/">Docs</a></li>
790
<li><a href="http://blog.nodejs.org">Blog</a></li>
791
<li><a href="/community/">Community</a></li>
792
<li><a href="/logos/">Logos</a></li>
793
<li><a href="http://jobs.nodejs.org/">Jobs</a></li>
794
<li><a href="http://twitter.com/nodejs" class="twitter">@nodejs</a></li>
797
<p>Copyright <a href="http://joyent.com">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.6.18/LICENSE">license</a>.</p>
800
<script src="../sh_main.js"></script>
801
<script src="../sh_javascript.min.js"></script>
802
<script>highlight(undefined, undefined, 'pre');</script>
804
var gaJsHost = (("https:" == document.location.protocol) ?
805
"https://ssl." : "http://www.");
806
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
810
var pageTracker = _gat._getTracker("UA-10874194-2");
811
pageTracker._trackPageview();
812
} catch(err) {}</script>