Skip to content

Commit 9581295

Browse files
thefourtheyeMylesBorins
authored andcommitted
doc: clarify fd behaviour with {read,write}File
PR-URL: #23706 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 4a79bef commit 9581295

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

doc/api/fs.md

+27-8
Original file line numberDiff line numberDiff line change
@@ -2547,14 +2547,18 @@ fs.readFile('<directory>', (err, data) => {
25472547
});
25482548
```
25492549

2550-
Any specified file descriptor has to support reading.
2551-
2552-
If a file descriptor is specified as the `path`, it will not be closed
2553-
automatically.
2554-
25552550
The `fs.readFile()` function buffers the entire file. To minimize memory costs,
25562551
when possible prefer streaming via `fs.createReadStream()`.
25572552

2553+
### File Descriptors
2554+
1. Any specified file descriptor has to support reading.
2555+
2. If a file descriptor is specified as the `path`, it will not be closed
2556+
automatically.
2557+
3. The reading will begin at the current position. For example, if the file
2558+
already had `'Hello World`' and six bytes are read with the file descriptor,
2559+
the call to `fs.readFile()` with the same file descriptor, would give
2560+
`'World'`, rather than `'Hello World'`.
2561+
25582562
## fs.readFileSync(path[, options])
25592563
<!-- YAML
25602564
added: v0.1.8
@@ -3540,14 +3544,19 @@ If `options` is a string, then it specifies the encoding:
35403544
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
35413545
```
35423546

3543-
Any specified file descriptor has to support writing.
3544-
35453547
It is unsafe to use `fs.writeFile()` multiple times on the same file without
35463548
waiting for the callback. For this scenario, [`fs.createWriteStream()`][] is
35473549
recommended.
35483550

3549-
If a file descriptor is specified as the `file`, it will not be closed
3551+
### File Descriptors
3552+
1. Any specified file descriptor has to support writing.
3553+
2. If a file descriptor is specified as the `file`, it will not be closed
35503554
automatically.
3555+
3. The writing will begin at the beginning of the file. For example, if the
3556+
file already had `'Hello World'` and the newly written content is `'Aloha'`,
3557+
then the contents of the file would be `'Aloha World'`, rather than just
3558+
`'Aloha'`.
3559+
35513560

35523561
## fs.writeFileSync(file, data[, options])
35533562
<!-- YAML
@@ -3780,6 +3789,11 @@ returned.
37803789

37813790
The `FileHandle` has to support reading.
37823791

3792+
If one or more `filehandle.read()` calls are made on a file handle and then a
3793+
`filehandle.readFile()` call is made, the data will be read from the current
3794+
position till the end of the file. It doesn't always read from the beginning
3795+
of the file.
3796+
37833797
#### filehandle.stat([options])
37843798
<!-- YAML
37853799
added: v10.0.0
@@ -3942,6 +3956,11 @@ The `FileHandle` has to support writing.
39423956
It is unsafe to use `filehandle.writeFile()` multiple times on the same file
39433957
without waiting for the `Promise` to be resolved (or rejected).
39443958

3959+
If one or more `filehandle.write()` calls are made on a file handle and then a
3960+
`filehandle.writeFile()` call is made, the data will be written from the
3961+
current position till the end of the file. It doesn't always write from the
3962+
beginning of the file.
3963+
39453964
### fsPromises.access(path[, mode])
39463965
<!-- YAML
39473966
added: v10.0.0

0 commit comments

Comments
 (0)