14d24bfe5a900f51fa227949528afd19440a4876
[platform/upstream/nodejs.git] / doc / api / fs.markdown
1 # File System
2
3     Stability: 2 - Stable
4
5 <!--name=fs-->
6
7 File I/O is provided by simple wrappers around standard POSIX functions.  To
8 use this module do `require('fs')`. All the methods have asynchronous and
9 synchronous forms.
10
11 The asynchronous form always take a completion callback as its last argument.
12 The arguments passed to the completion callback depend on the method, but the
13 first argument is always reserved for an exception. If the operation was
14 completed successfully, then the first argument will be `null` or `undefined`.
15
16 When using the synchronous form any exceptions are immediately thrown.
17 You can use try/catch to handle exceptions or allow them to bubble up.
18
19 Here is an example of the asynchronous version:
20
21     var fs = require('fs');
22
23     fs.unlink('/tmp/hello', function (err) {
24       if (err) throw err;
25       console.log('successfully deleted /tmp/hello');
26     });
27
28 Here is the synchronous version:
29
30     var fs = require('fs');
31
32     fs.unlinkSync('/tmp/hello');
33     console.log('successfully deleted /tmp/hello');
34
35 With the asynchronous methods there is no guaranteed ordering. So the
36 following is prone to error:
37
38     fs.rename('/tmp/hello', '/tmp/world', function (err) {
39       if (err) throw err;
40       console.log('renamed complete');
41     });
42     fs.stat('/tmp/world', function (err, stats) {
43       if (err) throw err;
44       console.log('stats: ' + JSON.stringify(stats));
45     });
46
47 It could be that `fs.stat` is executed before `fs.rename`.
48 The correct way to do this is to chain the callbacks.
49
50     fs.rename('/tmp/hello', '/tmp/world', function (err) {
51       if (err) throw err;
52       fs.stat('/tmp/world', function (err, stats) {
53         if (err) throw err;
54         console.log('stats: ' + JSON.stringify(stats));
55       });
56     });
57
58 In busy processes, the programmer is _strongly encouraged_ to use the
59 asynchronous versions of these calls. The synchronous versions will block
60 the entire process until they complete--halting all connections.
61
62 Relative path to filename can be used, remember however that this path will be
63 relative to `process.cwd()`.
64
65 Most fs functions let you omit the callback argument. If you do, a default
66 callback is used that rethrows errors. To get a trace to the original call
67 site, set the NODE_DEBUG environment variable:
68
69     $ cat script.js
70     function bad() {
71       require('fs').readFile('/');
72     }
73     bad();
74
75     $ env NODE_DEBUG=fs iojs script.js
76     fs.js:66
77             throw err;
78                   ^
79     Error: EISDIR, read
80         at rethrow (fs.js:61:21)
81         at maybeCallback (fs.js:79:42)
82         at Object.fs.readFile (fs.js:153:18)
83         at bad (/path/to/script.js:2:17)
84         at Object.<anonymous> (/path/to/script.js:5:1)
85         <etc.>
86
87
88 ## fs.rename(oldPath, newPath, callback)
89
90 Asynchronous rename(2). No arguments other than a possible exception are given
91 to the completion callback.
92
93 ## fs.renameSync(oldPath, newPath)
94
95 Synchronous rename(2).
96
97 ## fs.ftruncate(fd, len, callback)
98
99 Asynchronous ftruncate(2). No arguments other than a possible exception are
100 given to the completion callback.
101
102 ## fs.ftruncateSync(fd, len)
103
104 Synchronous ftruncate(2).
105
106 ## fs.truncate(path, len, callback)
107
108 Asynchronous truncate(2). No arguments other than a possible exception are
109 given to the completion callback. A file descriptor can also be passed as the
110 first argument. In this case, `fs.ftruncate()` is called.
111
112 ## fs.truncateSync(path, len)
113
114 Synchronous truncate(2).
115
116 ## fs.chown(path, uid, gid, callback)
117
118 Asynchronous chown(2). No arguments other than a possible exception are given
119 to the completion callback.
120
121 ## fs.chownSync(path, uid, gid)
122
123 Synchronous chown(2).
124
125 ## fs.fchown(fd, uid, gid, callback)
126
127 Asynchronous fchown(2). No arguments other than a possible exception are given
128 to the completion callback.
129
130 ## fs.fchownSync(fd, uid, gid)
131
132 Synchronous fchown(2).
133
134 ## fs.lchown(path, uid, gid, callback)
135
136 Asynchronous lchown(2). No arguments other than a possible exception are given
137 to the completion callback.
138
139 ## fs.lchownSync(path, uid, gid)
140
141 Synchronous lchown(2).
142
143 ## fs.chmod(path, mode, callback)
144
145 Asynchronous chmod(2). No arguments other than a possible exception are given
146 to the completion callback.
147
148 ## fs.chmodSync(path, mode)
149
150 Synchronous chmod(2).
151
152 ## fs.fchmod(fd, mode, callback)
153
154 Asynchronous fchmod(2). No arguments other than a possible exception
155 are given to the completion callback.
156
157 ## fs.fchmodSync(fd, mode)
158
159 Synchronous fchmod(2).
160
161 ## fs.lchmod(path, mode, callback)
162
163 Asynchronous lchmod(2). No arguments other than a possible exception
164 are given to the completion callback.
165
166 Only available on Mac OS X.
167
168 ## fs.lchmodSync(path, mode)
169
170 Synchronous lchmod(2).
171
172 ## fs.stat(path, callback)
173
174 Asynchronous stat(2). The callback gets two arguments `(err, stats)` where
175 `stats` is a [fs.Stats](#fs_class_fs_stats) object.  See the [fs.Stats](#fs_class_fs_stats)
176 section below for more information.
177
178 ## fs.lstat(path, callback)
179
180 Asynchronous lstat(2). The callback gets two arguments `(err, stats)` where
181 `stats` is a `fs.Stats` object. `lstat()` is identical to `stat()`, except that if
182 `path` is a symbolic link, then the link itself is stat-ed, not the file that it
183 refers to.
184
185 ## fs.fstat(fd, callback)
186
187 Asynchronous fstat(2). The callback gets two arguments `(err, stats)` where
188 `stats` is a `fs.Stats` object. `fstat()` is identical to `stat()`, except that
189 the file to be stat-ed is specified by the file descriptor `fd`.
190
191 ## fs.statSync(path)
192
193 Synchronous stat(2). Returns an instance of `fs.Stats`.
194
195 ## fs.lstatSync(path)
196
197 Synchronous lstat(2). Returns an instance of `fs.Stats`.
198
199 ## fs.fstatSync(fd)
200
201 Synchronous fstat(2). Returns an instance of `fs.Stats`.
202
203 ## fs.link(srcpath, dstpath, callback)
204
205 Asynchronous link(2). No arguments other than a possible exception are given to
206 the completion callback.
207
208 ## fs.linkSync(srcpath, dstpath)
209
210 Synchronous link(2).
211
212 ## fs.symlink(destination, path[, type], callback)
213
214 Asynchronous symlink(2). No arguments other than a possible exception are given
215 to the completion callback.
216 The `type` argument can be set to `'dir'`, `'file'`, or `'junction'` (default
217 is `'file'`) and is only available on Windows (ignored on other platforms).
218 Note that Windows junction points require the destination path to be absolute.  When using
219 `'junction'`, the `destination` argument will automatically be normalized to absolute path.
220
221 ## fs.symlinkSync(destination, path[, type])
222
223 Synchronous symlink(2).
224
225 ## fs.readlink(path, callback)
226
227 Asynchronous readlink(2). The callback gets two arguments `(err,
228 linkString)`.
229
230 ## fs.readlinkSync(path)
231
232 Synchronous readlink(2). Returns the symbolic link's string value.
233
234 ## fs.realpath(path[, cache], callback)
235
236 Asynchronous realpath(2). The `callback` gets two arguments `(err,
237 resolvedPath)`. May use `process.cwd` to resolve relative paths. `cache` is an
238 object literal of mapped paths that can be used to force a specific path
239 resolution or avoid additional `fs.stat` calls for known real paths.
240
241 Example:
242
243     var cache = {'/etc':'/private/etc'};
244     fs.realpath('/etc/passwd', cache, function (err, resolvedPath) {
245       if (err) throw err;
246       console.log(resolvedPath);
247     });
248
249 ## fs.realpathSync(path[, cache])
250
251 Synchronous realpath(2). Returns the resolved path.
252
253 ## fs.unlink(path, callback)
254
255 Asynchronous unlink(2). No arguments other than a possible exception are given
256 to the completion callback.
257
258 ## fs.unlinkSync(path)
259
260 Synchronous unlink(2).
261
262 ## fs.rmdir(path, callback)
263
264 Asynchronous rmdir(2). No arguments other than a possible exception are given
265 to the completion callback.
266
267 ## fs.rmdirSync(path)
268
269 Synchronous rmdir(2).
270
271 ## fs.mkdir(path[, mode], callback)
272
273 Asynchronous mkdir(2). No arguments other than a possible exception are given
274 to the completion callback. `mode` defaults to `0o777`.
275
276 ## fs.mkdirSync(path[, mode])
277
278 Synchronous mkdir(2).
279
280 ## fs.readdir(path, callback)
281
282 Asynchronous readdir(3).  Reads the contents of a directory.
283 The callback gets two arguments `(err, files)` where `files` is an array of
284 the names of the files in the directory excluding `'.'` and `'..'`.
285
286 ## fs.readdirSync(path)
287
288 Synchronous readdir(3). Returns an array of filenames excluding `'.'` and
289 `'..'`.
290
291 ## fs.close(fd, callback)
292
293 Asynchronous close(2).  No arguments other than a possible exception are given
294 to the completion callback.
295
296 ## fs.closeSync(fd)
297
298 Synchronous close(2).
299
300 ## fs.open(path, flags[, mode], callback)
301
302 Asynchronous file open. See open(2). `flags` can be:
303
304 * `'r'` - Open file for reading.
305 An exception occurs if the file does not exist.
306
307 * `'r+'` - Open file for reading and writing.
308 An exception occurs if the file does not exist.
309
310 * `'rs'` - Open file for reading in synchronous mode. Instructs the operating
311   system to bypass the local file system cache.
312
313   This is primarily useful for opening files on NFS mounts as it allows you to
314   skip the potentially stale local cache. It has a very real impact on I/O
315   performance so don't use this flag unless you need it.
316
317   Note that this doesn't turn `fs.open()` into a synchronous blocking call.
318   If that's what you want then you should be using `fs.openSync()`
319
320 * `'rs+'` - Open file for reading and writing, telling the OS to open it
321   synchronously. See notes for `'rs'` about using this with caution.
322
323 * `'w'` - Open file for writing.
324 The file is created (if it does not exist) or truncated (if it exists).
325
326 * `'wx'` - Like `'w'` but fails if `path` exists.
327
328 * `'w+'` - Open file for reading and writing.
329 The file is created (if it does not exist) or truncated (if it exists).
330
331 * `'wx+'` - Like `'w+'` but fails if `path` exists.
332
333 * `'a'` - Open file for appending.
334 The file is created if it does not exist.
335
336 * `'ax'` - Like `'a'` but fails if `path` exists.
337
338 * `'a+'` - Open file for reading and appending.
339 The file is created if it does not exist.
340
341 * `'ax+'` - Like `'a+'` but fails if `path` exists.
342
343 `mode` sets the file mode (permission and sticky bits), but only if the file was
344 created. It defaults to `0666`, readable and writeable.
345
346 The callback gets two arguments `(err, fd)`.
347
348 The exclusive flag `'x'` (`O_EXCL` flag in open(2)) ensures that `path` is newly
349 created. On POSIX systems, `path` is considered to exist even if it is a symlink
350 to a non-existent file. The exclusive flag may or may not work with network file
351 systems.
352
353 On Linux, positional writes don't work when the file is opened in append mode.
354 The kernel ignores the position argument and always appends the data to
355 the end of the file.
356
357 ## fs.openSync(path, flags[, mode])
358
359 Synchronous version of `fs.open()`.
360
361 ## fs.utimes(path, atime, mtime, callback)
362 ## fs.utimesSync(path, atime, mtime)
363
364 Change file timestamps of the file referenced by the supplied path.
365
366 ## fs.futimes(fd, atime, mtime, callback)
367 ## fs.futimesSync(fd, atime, mtime)
368
369 Change the file timestamps of a file referenced by the supplied file
370 descriptor.
371
372 ## fs.fsync(fd, callback)
373
374 Asynchronous fsync(2). No arguments other than a possible exception are given
375 to the completion callback.
376
377 ## fs.fsyncSync(fd)
378
379 Synchronous fsync(2).
380
381 ## fs.write(fd, buffer, offset, length[, position], callback)
382
383 Write `buffer` to the file specified by `fd`.
384
385 `offset` and `length` determine the part of the buffer to be written.
386
387 `position` refers to the offset from the beginning of the file where this data
388 should be written. If `typeof position !== 'number'`, the data will be written
389 at the current position. See pwrite(2).
390
391 The callback will be given three arguments `(err, written, buffer)` where
392 `written` specifies how many _bytes_ were written from `buffer`.
393
394 Note that it is unsafe to use `fs.write` multiple times on the same file
395 without waiting for the callback. For this scenario,
396 `fs.createWriteStream` is strongly recommended.
397
398 On Linux, positional writes don't work when the file is opened in append mode.
399 The kernel ignores the position argument and always appends the data to
400 the end of the file.
401
402 ## fs.write(fd, data[, position[, encoding]], callback)
403
404 Write `data` to the file specified by `fd`.  If `data` is not a Buffer instance
405 then the value will be coerced to a string.
406
407 `position` refers to the offset from the beginning of the file where this data
408 should be written. If `typeof position !== 'number'` the data will be written at
409 the current position. See pwrite(2).
410
411 `encoding` is the expected string encoding.
412
413 The callback will receive the arguments `(err, written, string)` where `written`
414 specifies how many _bytes_ the passed string required to be written. Note that
415 bytes written is not the same as string characters. See
416 [Buffer.byteLength](buffer.html#buffer_class_method_buffer_bytelength_string_encoding).
417
418 Unlike when writing `buffer`, the entire string must be written. No substring
419 may be specified. This is because the byte offset of the resulting data may not
420 be the same as the string offset.
421
422 Note that it is unsafe to use `fs.write` multiple times on the same file
423 without waiting for the callback. For this scenario,
424 `fs.createWriteStream` is strongly recommended.
425
426 On Linux, positional writes don't work when the file is opened in append mode.
427 The kernel ignores the position argument and always appends the data to
428 the end of the file.
429
430 ## fs.writeSync(fd, buffer, offset, length[, position])
431
432 ## fs.writeSync(fd, data[, position[, encoding]])
433
434 Synchronous versions of `fs.write()`. Returns the number of bytes written.
435
436 ## fs.read(fd, buffer, offset, length, position, callback)
437
438 Read data from the file specified by `fd`.
439
440 `buffer` is the buffer that the data will be written to.
441
442 `offset` is the offset in the buffer to start writing at.
443
444 `length` is an integer specifying the number of bytes to read.
445
446 `position` is an integer specifying where to begin reading from in the file.
447 If `position` is `null`, data will be read from the current file position.
448
449 The callback is given the three arguments, `(err, bytesRead, buffer)`.
450
451 ## fs.readSync(fd, buffer, offset, length, position)
452
453 Synchronous version of `fs.read`. Returns the number of `bytesRead`.
454
455 ## fs.readFile(filename[, options], callback)
456
457 * `filename` {String}
458 * `options` {Object}
459   * `encoding` {String | Null} default = `null`
460   * `flag` {String} default = `'r'`
461 * `callback` {Function}
462
463 Asynchronously reads the entire contents of a file. Example:
464
465     fs.readFile('/etc/passwd', function (err, data) {
466       if (err) throw err;
467       console.log(data);
468     });
469
470 The callback is passed two arguments `(err, data)`, where `data` is the
471 contents of the file.
472
473 If no encoding is specified, then the raw buffer is returned.
474
475
476 ## fs.readFileSync(filename[, options])
477
478 Synchronous version of `fs.readFile`. Returns the contents of the `filename`.
479
480 If the `encoding` option is specified then this function returns a
481 string. Otherwise it returns a buffer.
482
483
484 ## fs.writeFile(filename, data[, options], callback)
485
486 * `filename` {String}
487 * `data` {String | Buffer}
488 * `options` {Object}
489   * `encoding` {String | Null} default = `'utf8'`
490   * `mode` {Number} default = `0o666`
491   * `flag` {String} default = `'w'`
492 * `callback` {Function}
493
494 Asynchronously writes data to a file, replacing the file if it already exists.
495 `data` can be a string or a buffer.
496
497 The `encoding` option is ignored if `data` is a buffer. It defaults
498 to `'utf8'`.
499
500 Example:
501
502     fs.writeFile('message.txt', 'Hello io.js', function (err) {
503       if (err) throw err;
504       console.log('It\'s saved!');
505     });
506
507 ## fs.writeFileSync(filename, data[, options])
508
509 The synchronous version of `fs.writeFile`.
510
511 ## fs.appendFile(filename, data[, options], callback)
512
513 * `filename` {String}
514 * `data` {String | Buffer}
515 * `options` {Object}
516   * `encoding` {String | Null} default = `'utf8'`
517   * `mode` {Number} default = `0o666`
518   * `flag` {String} default = `'a'`
519 * `callback` {Function}
520
521 Asynchronously append data to a file, creating the file if it does not yet exist.
522 `data` can be a string or a buffer.
523
524 Example:
525
526     fs.appendFile('message.txt', 'data to append', function (err) {
527       if (err) throw err;
528       console.log('The "data to append" was appended to file!');
529     });
530
531 ## fs.appendFileSync(filename, data[, options])
532
533 The synchronous version of `fs.appendFile`.
534
535 ## fs.watchFile(filename[, options], listener)
536
537     Stability: 2 - Unstable.  Use fs.watch instead, if possible.
538
539 Watch for changes on `filename`. The callback `listener` will be called each
540 time the file is accessed.
541
542 The second argument is optional. The `options` if provided should be an object
543 containing two members a boolean, `persistent`, and `interval`. `persistent`
544 indicates whether the process should continue to run as long as files are
545 being watched. `interval` indicates how often the target should be polled,
546 in milliseconds. The default is `{ persistent: true, interval: 5007 }`.
547
548 The `listener` gets two arguments the current stat object and the previous
549 stat object:
550
551     fs.watchFile('message.text', function (curr, prev) {
552       console.log('the current mtime is: ' + curr.mtime);
553       console.log('the previous mtime was: ' + prev.mtime);
554     });
555
556 These stat objects are instances of `fs.Stat`.
557
558 If you want to be notified when the file was modified, not just accessed
559 you need to compare `curr.mtime` and `prev.mtime`.
560
561 ## fs.unwatchFile(filename[, listener])
562
563     Stability: 2 - Unstable.  Use fs.watch instead, if possible.
564
565 Stop watching for changes on `filename`. If `listener` is specified, only that
566 particular listener is removed. Otherwise, *all* listeners are removed and you
567 have effectively stopped watching `filename`.
568
569 Calling `fs.unwatchFile()` with a filename that is not being watched is a
570 no-op, not an error.
571
572 ## fs.watch(filename[, options][, listener])
573
574     Stability: 2 - Unstable.
575
576 Watch for changes on `filename`, where `filename` is either a file or a
577 directory.  The returned object is a [fs.FSWatcher](#fs_class_fs_fswatcher).
578
579 The second argument is optional. The `options` if provided should be an object.
580 The supported boolean members are `persistent` and `recursive`. `persistent`
581 indicates whether the process should continue to run as long as files are being
582 watched. `recursive` indicates whether all subdirectories should be watched, or
583 only the current directory. This applies when a directory is specified, and only
584 on supported platforms (See Caveats below).
585
586 The default is `{ persistent: true, recursive: false }`.
587
588 The listener callback gets two arguments `(event, filename)`.  `event` is either
589 'rename' or 'change', and `filename` is the name of the file which triggered
590 the event.
591
592 ### Caveats
593
594 <!--type=misc-->
595
596 The `fs.watch` API is not 100% consistent across platforms, and is
597 unavailable in some situations.
598
599 The recursive option is currently supported on OS X. Only FSEvents supports this
600 type of file watching so it is unlikely any additional platforms will be added
601 soon.
602
603 #### Availability
604
605 <!--type=misc-->
606
607 This feature depends on the underlying operating system providing a way
608 to be notified of filesystem changes.
609
610 * On Linux systems, this uses `inotify`.
611 * On BSD systems, this uses `kqueue`.
612 * On OS X, this uses `kqueue` for files and 'FSEvents' for directories.
613 * On SunOS systems (including Solaris and SmartOS), this uses `event ports`.
614 * On Windows systems, this feature depends on `ReadDirectoryChangesW`.
615
616 If the underlying functionality is not available for some reason, then
617 `fs.watch` will not be able to function.  For example, watching files or
618 directories on network file systems (NFS, SMB, etc.) often doesn't work
619 reliably or at all.
620
621 You can still use `fs.watchFile`, which uses stat polling, but it is slower and
622 less reliable.
623
624 #### Filename Argument
625
626 <!--type=misc-->
627
628 Providing `filename` argument in the callback is not supported
629 on every platform (currently it's only supported on Linux and Windows).  Even
630 on supported platforms `filename` is not always guaranteed to be provided.
631 Therefore, don't assume that `filename` argument is always provided in the
632 callback, and have some fallback logic if it is null.
633
634     fs.watch('somedir', function (event, filename) {
635       console.log('event is: ' + event);
636       if (filename) {
637         console.log('filename provided: ' + filename);
638       } else {
639         console.log('filename not provided');
640       }
641     });
642
643 ## fs.exists(path, callback)
644
645 Test whether or not the given path exists by checking with the file system.
646 Then call the `callback` argument with either true or false.  Example:
647
648     fs.exists('/etc/passwd', function (exists) {
649       util.debug(exists ? "it's there" : "no passwd!");
650     });
651
652 `fs.exists()` is an anachronism and exists only for historical reasons.
653 There should almost never be a reason to use it in your own code.
654
655 In particular, checking if a file exists before opening it is an anti-pattern
656 that leaves you vulnerable to race conditions: another process may remove the
657 file between the calls to `fs.exists()` and `fs.open()`.  Just open the file
658 and handle the error when it's not there.
659
660 `fs.exists()` is **deprecated**.
661
662 ## fs.existsSync(path)
663
664 Synchronous version of `fs.exists`.
665
666 `fs.existsSync()` is **deprecated**.
667
668 ## fs.access(path[, mode], callback)
669
670 Tests a user's permissions for the file specified by `path`. `mode` is an
671 optional integer that specifies the accessibility checks to be performed. The
672 following constants define the possible values of `mode`. It is possible to
673 create a mask consisting of the bitwise OR of two or more values.
674
675 - `fs.F_OK` - File is visible to the calling process. This is useful for
676 determining if a file exists, but says nothing about `rwx` permissions.
677 Default if no `mode` is specified.
678 - `fs.R_OK` - File can be read by the calling process.
679 - `fs.W_OK` - File can be written by the calling process.
680 - `fs.X_OK` - File can be executed by the calling process. This has no effect
681 on Windows (will behave like `fs.F_OK`).
682
683 The final argument, `callback`, is a callback function that is invoked with
684 a possible error argument. If any of the accessibility checks fail, the error
685 argument will be populated. The following example checks if the file
686 `/etc/passwd` can be read and written by the current process.
687
688     fs.access('/etc/passwd', fs.R_OK | fs.W_OK, function(err) {
689       util.debug(err ? 'no access!' : 'can read/write');
690     });
691
692 ## fs.accessSync(path[, mode])
693
694 Synchronous version of `fs.access`. This throws if any accessibility checks
695 fail, and does nothing otherwise.
696
697 ## Class: fs.Stats
698
699 Objects returned from `fs.stat()`, `fs.lstat()` and `fs.fstat()` and their
700 synchronous counterparts are of this type.
701
702  - `stats.isFile()`
703  - `stats.isDirectory()`
704  - `stats.isBlockDevice()`
705  - `stats.isCharacterDevice()`
706  - `stats.isSymbolicLink()` (only valid with  `fs.lstat()`)
707  - `stats.isFIFO()`
708  - `stats.isSocket()`
709
710 For a regular file `util.inspect(stats)` would return a string very
711 similar to this:
712
713     { dev: 2114,
714       ino: 48064969,
715       mode: 33188,
716       nlink: 1,
717       uid: 85,
718       gid: 100,
719       rdev: 0,
720       size: 527,
721       blksize: 4096,
722       blocks: 8,
723       atime: Mon, 10 Oct 2011 23:24:11 GMT,
724       mtime: Mon, 10 Oct 2011 23:24:11 GMT,
725       ctime: Mon, 10 Oct 2011 23:24:11 GMT,
726       birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
727
728 Please note that `atime`, `mtime`, `birthtime`, and `ctime` are
729 instances of [Date][MDN-Date] object and to compare the values of
730 these objects you should use appropriate methods. For most general
731 uses [getTime()][MDN-Date-getTime] will return the number of
732 milliseconds elapsed since _1 January 1970 00:00:00 UTC_ and this
733 integer should be sufficient for any comparison, however there are
734 additional methods which can be used for displaying fuzzy information.
735 More details can be found in the [MDN JavaScript Reference][MDN-Date]
736 page.
737
738 [MDN-Date]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
739 [MDN-Date-getTime]: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime
740
741 ### Stat Time Values
742
743 The times in the stat object have the following semantics:
744
745 * `atime` "Access Time" - Time when file data last accessed.  Changed
746   by the `mknod(2)`, `utimes(2)`, and `read(2)` system calls.
747 * `mtime` "Modified Time" - Time when file data last modified.
748   Changed by the `mknod(2)`, `utimes(2)`, and `write(2)` system calls.
749 * `ctime` "Change Time" - Time when file status was last changed
750   (inode data modification).  Changed by the `chmod(2)`, `chown(2)`,
751   `link(2)`, `mknod(2)`, `rename(2)`, `unlink(2)`, `utimes(2)`,
752   `read(2)`, and `write(2)` system calls.
753 * `birthtime` "Birth Time" -  Time of file creation. Set once when the
754   file is created.  On filesystems where birthtime is not available,
755   this field may instead hold either the `ctime` or
756   `1970-01-01T00:00Z` (ie, unix epoch timestamp `0`).  On Darwin and
757   other FreeBSD variants, also set if the `atime` is explicitly set to
758   an earlier value than the current `birthtime` using the `utimes(2)`
759   system call.
760
761 Prior to io.js v1.0 and Node v0.12, the `ctime` held the `birthtime` on Windows
762 systems.  Note that as of v0.12, `ctime` is not "creation time", and
763 on Unix systems, it never was.
764
765 ## fs.createReadStream(path[, options])
766
767 Returns a new ReadStream object (See `Readable Stream`).
768
769 `options` is an object with the following defaults:
770
771     { flags: 'r',
772       encoding: null,
773       fd: null,
774       mode: 0o666,
775       autoClose: true
776     }
777
778 `options` can include `start` and `end` values to read a range of bytes from
779 the file instead of the entire file.  Both `start` and `end` are inclusive and
780 start at 0. The `encoding` can be `'utf8'`, `'ascii'`, or `'base64'`.
781
782 If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
783 the specified file descriptor. This means that no `open` event will be emitted.
784
785 If `autoClose` is false, then the file descriptor won't be closed, even if
786 there's an error.  It is your responsibility to close it and make sure
787 there's no file descriptor leak.  If `autoClose` is set to true (default
788 behavior), on `error` or `end` the file descriptor will be closed
789 automatically.
790
791 An example to read the last 10 bytes of a file which is 100 bytes long:
792
793     fs.createReadStream('sample.txt', {start: 90, end: 99});
794
795
796 ## Class: fs.ReadStream
797
798 `ReadStream` is a [Readable Stream](stream.html#stream_class_stream_readable).
799
800 ### Event: 'open'
801
802 * `fd` {Integer} file descriptor used by the ReadStream.
803
804 Emitted when the ReadStream's file is opened.
805
806
807 ## fs.createWriteStream(path[, options])
808
809 Returns a new WriteStream object (See `Writable Stream`).
810
811 `options` is an object with the following defaults:
812
813     { flags: 'w',
814       encoding: null,
815       fd: null,
816       mode: 0o666 }
817
818 `options` may also include a `start` option to allow writing data at
819 some position past the beginning of the file.  Modifying a file rather
820 than replacing it may require a `flags` mode of `r+` rather than the
821 default mode `w`.
822
823 Like `ReadStream` above, if `fd` is specified, `WriteStream` will ignore the
824 `path` argument and will use the specified file descriptor. This means that no
825 `open` event will be emitted.
826
827
828 ## Class: fs.WriteStream
829
830 `WriteStream` is a [Writable Stream](stream.html#stream_class_stream_writable).
831
832 ### Event: 'open'
833
834 * `fd` {Integer} file descriptor used by the WriteStream.
835
836 Emitted when the WriteStream's file is opened.
837
838 ### file.bytesWritten
839
840 The number of bytes written so far. Does not include data that is still queued
841 for writing.
842
843 ## Class: fs.FSWatcher
844
845 Objects returned from `fs.watch()` are of this type.
846
847 ### watcher.close()
848
849 Stop watching for changes on the given `fs.FSWatcher`.
850
851 ### Event: 'change'
852
853 * `event` {String} The type of fs change
854 * `filename` {String} The filename that changed (if relevant/available)
855
856 Emitted when something changes in a watched directory or file.
857 See more details in [fs.watch](#fs_fs_watch_filename_options_listener).
858
859 ### Event: 'error'
860
861 * `error` {Error object}
862
863 Emitted when an error occurs.