Skip to content

Commit 351d9df

Browse files
committed
doc: clarify fd behaviour with {read,write}File
1 parent cf3f8dd commit 351d9df

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

doc/api/fs.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,10 +2547,13 @@ 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
2550+
### File Descriptors
2551+
1. Any specified file descriptor has to support reading.
2552+
2. If a file descriptor is specified as the `path`, it will not be closed
25532553
automatically.
2554+
3. The reading will begin at the current position. If the file size is
2555+
10 bytes and if six bytes are already read with this file descriptor, then
2556+
`readFile` will return only the rest of the four bytes.
25542557

25552558
The `fs.readFile()` function buffers the entire file. To minimize memory costs,
25562559
when possible prefer streaming via `fs.createReadStream()`.
@@ -3540,14 +3543,21 @@ If `options` is a string, then it specifies the encoding:
35403543
fs.writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
35413544
```
35423545

3543-
Any specified file descriptor has to support writing.
3546+
### File Descriptors
3547+
1. Any specified file descriptor has to support writing.
3548+
2. If a file descriptor is specified as the `file`, it will not be closed
3549+
automatically.
3550+
3. The writing will begin at the beginning of the file. If the file size
3551+
is 10 bytes and if six bytes are written with this file descriptor, then
3552+
`writeFile` will return six bytes newly written and four bytes from the file.
3553+
For example, if the file already had `'Hello World'` and the newly written
3554+
content is `'Aloha'`, then the contents of the file would be `'Aloha World'`,
3555+
rather than just `'Aloha'`.
35443556

35453557
It is unsafe to use `fs.writeFile()` multiple times on the same file without
35463558
waiting for the callback. For this scenario, [`fs.createWriteStream()`][] is
35473559
recommended.
35483560

3549-
If a file descriptor is specified as the `file`, it will not be closed
3550-
automatically.
35513561

35523562
## fs.writeFileSync(file, data[, options])
35533563
<!-- YAML

0 commit comments

Comments
 (0)