Skip to content

Commit da494ef

Browse files
thefourtheyetargos
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 539e123 commit da494ef

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
@@ -2554,14 +2554,18 @@ fs.readFile('<directory>', (err, data) => {
25542554
});
25552555
```
25562556

2557-
Any specified file descriptor has to support reading.
2558-
2559-
If a file descriptor is specified as the `path`, it will not be closed
2560-
automatically.
2561-
25622557
The `fs.readFile()` function buffers the entire file. To minimize memory costs,
25632558
when possible prefer streaming via `fs.createReadStream()`.
25642559

2560+
### File Descriptors
2561+
1. Any specified file descriptor has to support reading.
2562+
2. If a file descriptor is specified as the `path`, it will not be closed
2563+
automatically.
2564+
3. The reading will begin at the current position. For example, if the file
2565+
already had `'Hello World`' and six bytes are read with the file descriptor,
2566+
the call to `fs.readFile()` with the same file descriptor, would give
2567+
`'World'`, rather than `'Hello World'`.
2568+
25652569
## fs.readFileSync(path[, options])
25662570
<!-- YAML
25672571
added: v0.1.8
@@ -3547,14 +3551,19 @@ If `options` is a string, then it specifies the encoding:
35473551
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
35483552
```
35493553

3550-
Any specified file descriptor has to support writing.
3551-
35523554
It is unsafe to use `fs.writeFile()` multiple times on the same file without
35533555
waiting for the callback. For this scenario, [`fs.createWriteStream()`][] is
35543556
recommended.
35553557

3556-
If a file descriptor is specified as the `file`, it will not be closed
3558+
### File Descriptors
3559+
1. Any specified file descriptor has to support writing.
3560+
2. If a file descriptor is specified as the `file`, it will not be closed
35573561
automatically.
3562+
3. The writing will begin at the beginning of the file. For example, if the
3563+
file already had `'Hello World'` and the newly written content is `'Aloha'`,
3564+
then the contents of the file would be `'Aloha World'`, rather than just
3565+
`'Aloha'`.
3566+
35583567

35593568
## fs.writeFileSync(file, data[, options])
35603569
<!-- YAML
@@ -3787,6 +3796,11 @@ returned.
37873796

37883797
The `FileHandle` has to support reading.
37893798

3799+
If one or more `filehandle.read()` calls are made on a file handle and then a
3800+
`filehandle.readFile()` call is made, the data will be read from the current
3801+
position till the end of the file. It doesn't always read from the beginning
3802+
of the file.
3803+
37903804
#### filehandle.stat([options])
37913805
<!-- YAML
37923806
added: v10.0.0
@@ -3949,6 +3963,11 @@ The `FileHandle` has to support writing.
39493963
It is unsafe to use `filehandle.writeFile()` multiple times on the same file
39503964
without waiting for the `Promise` to be resolved (or rejected).
39513965

3966+
If one or more `filehandle.write()` calls are made on a file handle and then a
3967+
`filehandle.writeFile()` call is made, the data will be written from the
3968+
current position till the end of the file. It doesn't always write from the
3969+
beginning of the file.
3970+
39523971
### fsPromises.access(path[, mode])
39533972
<!-- YAML
39543973
added: v10.0.0

0 commit comments

Comments
 (0)