trait FileHandle extends Object with StObject with _TransferListItem
- Annotations
- @JSType() @native()
- Alphabetic
- By Inheritance
- FileHandle
- _TransferListItem
- StObject
- Object
- Any
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def appendFile(data: Uint8Array, options: BufferEncoding): Promise[Unit]
- def appendFile(data: Uint8Array, options: ObjectEncodingOptions with FlagAndOpenMode): Promise[Unit]
- def appendFile(data: Uint8Array): Promise[Unit]
- def appendFile(data: String, options: BufferEncoding): Promise[Unit]
- def appendFile(data: String, options: ObjectEncodingOptions with FlagAndOpenMode): Promise[Unit]
- def appendFile(data: String): Promise[Unit]
Alias of
filehandle.writeFile()
.Alias of
filehandle.writeFile()
.When operating on file handles, the mode cannot be changed from what it was set to with
fsPromises.open()
. Therefore, this is equivalent tofilehandle.writeFile()
.- returns
Fulfills with
undefined
upon success.
- Since
v10.0.0
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def chmod(mode: Mode): Promise[Unit]
Modifies the permissions on the file.
Modifies the permissions on the file. See [
chmod(2)
](http://man7.org/linux/man-pages/man2/chmod.2.html).- mode
the file mode bit mask.
- returns
Fulfills with
undefined
upon success.
- Since
v10.0.0
- def chown(uid: Double, gid: Double): Promise[Unit]
Changes the ownership of the file.
Changes the ownership of the file. A wrapper for [
chown(2)
](http://man7.org/linux/man-pages/man2/chown.2.html).- uid
The file's new owner's user id.
- gid
The file's new group's group id.
- returns
Fulfills with
undefined
upon success.
- Since
v10.0.0
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native() @HotSpotIntrinsicCandidate()
- def close(): Promise[Unit]
Closes the file handle after waiting for any pending operation on the handle to complete.
Closes the file handle after waiting for any pending operation on the handle to complete.
js import { open } from 'fs/promises';
let filehandle; try { filehandle = await open('thefile.txt', 'r'); } finally { await filehandle?.close(); }
- returns
Fulfills with
undefined
upon success.
- Since
v10.0.0
- def createReadStream(options: CreateReadStreamOptions): ReadStream
- def createReadStream(): ReadStream
Unlike the 16 kb default
highWaterMark
for astream.Readable
, the stream returned by this method has a defaulthighWaterMark
of 64 kb.Unlike the 16 kb default
highWaterMark
for astream.Readable
, the stream returned by this method has a defaulthighWaterMark
of 64 kb.options
can includestart
andend
values to read a range of bytes from the file instead of the entire file. Bothstart
andend
are inclusive and start counting at 0, allowed values are in the \[0, [Number.MAX_SAFE_INTEGER
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Ifstart
is omitted orundefined
,filehandle.createReadStream()
reads sequentially from the current file position. Theencoding
can be any one of those accepted byBuffer
.If the
FileHandle
points to a character device that only supports blocking reads (such as keyboard or sound card), read operations do not finish until data is available. This can prevent the process from exiting and the stream from closing naturally.By default, the stream will emit a
'close'
event after it has been destroyed. Set theemitClose
option tofalse
to change this behavior.js import { open } from 'fs/promises';
const fd = await open('/dev/input/event0'); // Create a stream from some character device. const stream = fd.createReadStream(); setTimeout(() => { stream.close(); // This may not close the stream. // Artificially marking end-of-stream, as if the underlying resource had // indicated end-of-file by itself, allows the stream to close. // This does not cancel pending read operations, and if there is such an // operation, the process may still not be able to exit successfully // until it finishes. stream.push(null); stream.read(0); }, 100);
If
autoClose
is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. IfautoClose
is set to true (default behavior), on'error'
or'end'
the file descriptor will be closed automatically.An example to read the last 10 bytes of a file which is 100 bytes long:
js import { open } from 'fs/promises';
const fd = await open('sample.txt'); fd.createReadStream({ start: 90, end: 99 });
- Since
v16.11.0
- def createWriteStream(options: CreateWriteStreamOptions): WriteStream
- def createWriteStream(): WriteStream
options
may also include astart
option to allow writing data at some position past the beginning of the file, allowed values are in the \[0, [Number.MAX_SAFE_INTEGER
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range.options
may also include astart
option to allow writing data at some position past the beginning of the file, allowed values are in the \[0, [Number.MAX_SAFE_INTEGER
](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Modifying a file rather than replacing it may require theflags
open
option to be set tor+
rather than the defaultr
. Theencoding
can be any one of those accepted byBuffer
.If
autoClose
is set to true (default behavior) on'error'
or'finish'
the file descriptor will be closed automatically. IfautoClose
is false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak.By default, the stream will emit a
'close'
event after it has been destroyed. Set theemitClose
option tofalse
to change this behavior.- Since
v16.11.0
- def datasync(): Promise[Unit]
Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state.
Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state. Refer to the POSIX [
fdatasync(2)
](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details.Unlike
filehandle.sync
this method does not flush modified metadata.- returns
Fulfills with
undefined
upon success.
- Since
v10.0.0
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- val fd: Double
The numeric file descriptor managed by the {FileHandle} object.
The numeric file descriptor managed by the {FileHandle} object.
- Since
v10.0.0
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def hasOwnProperty(v: String): Boolean
- Definition Classes
- Object
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def isPrototypeOf(v: Object): Boolean
- Definition Classes
- Object
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @HotSpotIntrinsicCandidate()
- def propertyIsEnumerable(v: String): Boolean
- Definition Classes
- Object
- def read[T](options: FileReadOptions[T]): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Unit, length: Unit, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Unit, length: Null, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Unit, length: Double, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Unit, length: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Null, length: Unit, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Null, length: Null, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Null, length: Double, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Null, length: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Double, length: Unit, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Double, length: Null, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Double, length: Double, position: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Double, length: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T, offset: Double): Promise[FileReadResult[T]]
- def read[T](buffer: T): Promise[FileReadResult[T]]
Reads data from the file and stores that in the given buffer.
Reads data from the file and stores that in the given buffer.
If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.
- buffer
A buffer that will be filled with the file data read.
- returns
Fulfills upon success with an object with two properties:
- Since
v10.0.0
- def read[T](): Promise[FileReadResult[T]]
- def readFile(options: BufferEncoding): Promise[String]
- def readFile(options: 3): Promise[Buffer]
- def readFile(options: ObjectEncodingOptionsflagFlag): Promise[|[String, Buffer]]
- def readFile(options: EncodingBufferEncoding): Promise[String]
Asynchronously reads the entire contents of a file.
Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically. The
FileHandle
must have been opened for reading.- options
An object that may contain an optional flag. If a flag is not provided, it defaults to
'r'
.
- def readFile(): Promise[Buffer]
Asynchronously reads the entire contents of a file.
Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically. The
FileHandle
must have been opened for reading. - def readv(buffers: Array[ArrayBufferView], position: Double): Promise[ReadVResult]
- def readv(buffers: Array[ArrayBufferView]): Promise[ReadVResult]
Read from a file and write to an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s
Read from a file and write to an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s
- returns
Fulfills upon success an object containing two properties:
- Since
v13.13.0, v12.17.0
- def stat(opts: StatOptions): Promise[|[Stats, BigIntStats]]
- def stat(opts: StatOptionsbiginttrue): Promise[BigIntStats]
- def stat(opts: StatOptionsbigintfalseund): Promise[Stats]
- def stat(): Promise[Stats]
- returns
Fulfills with an {fs.Stats} for the file.
- Since
v10.0.0
- def sync(): Promise[Unit]
Request that all data for the open file descriptor is flushed to the storage device.
Request that all data for the open file descriptor is flushed to the storage device. The specific implementation is operating system and device specific. Refer to the POSIX [
fsync(2)
](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail.- returns
Fufills with
undefined
upon success.
- Since
v10.0.0
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toLocaleString(): String
- Definition Classes
- Object
- def toString(): String
- Definition Classes
- AnyRef → Any
- def truncate(len: Double): Promise[Unit]
- def truncate(): Promise[Unit]
Truncates the file.
Truncates the file.
If the file was larger than
len
bytes, only the firstlen
bytes will be retained in the file.The following example retains only the first four bytes of the file:
js import { open } from 'fs/promises';
let filehandle = null; try { filehandle = await open('temp.txt', 'r+'); await filehandle.truncate(4); } finally { await filehandle?.close(); }
If the file previously was shorter than
len
bytes, it is extended, and the extended part is filled with null bytes ('\0'
):If
len
is negative then0
will be used.- returns
Fulfills with
undefined
upon success.
- Since
v10.0.0
- def utimes(atime: Double, mtime: Double): Promise[Unit]
- def utimes(atime: Double, mtime: Date): Promise[Unit]
- def utimes(atime: Double, mtime: String): Promise[Unit]
- def utimes(atime: Date, mtime: Double): Promise[Unit]
- def utimes(atime: Date, mtime: Date): Promise[Unit]
- def utimes(atime: Date, mtime: String): Promise[Unit]
- def utimes(atime: String, mtime: Double): Promise[Unit]
- def utimes(atime: String, mtime: Date): Promise[Unit]
- def utimes(atime: String, mtime: String): Promise[Unit]
Change the file system timestamps of the object referenced by the
FileHandle
then resolves the promise with no arguments upon success.Change the file system timestamps of the object referenced by the
FileHandle
then resolves the promise with no arguments upon success.- Since
v10.0.0
- def valueOf(): Any
- Definition Classes
- Object
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- def write[TBuffer](buffer: TBuffer, offset: Unit, length: Unit, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Unit, length: Null, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Unit, length: Double, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Unit, length: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Null, length: Unit, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Null, length: Null, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Null, length: Double, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Null, length: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Double, length: Unit, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Double, length: Null, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Double, length: Double, position: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Double, length: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer, offset: Double): Promise[Buffer[TBuffer]]
- def write[TBuffer](buffer: TBuffer): Promise[Buffer[TBuffer]]
Write
buffer
to the file.Write
buffer
to the file.If
buffer
is a plain object, it must have an own (not inherited)toString
function property.The promise is resolved with an object containing two properties:
It is unsafe to use
filehandle.write()
multiple times on the same file without waiting for the promise to be resolved (or rejected). For this scenario, usefs.createWriteStream()
.On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
- Since
v10.0.0
- def write(data: String, position: Unit, encoding: BufferEncoding): Promise[BytesWritten]
- def write(data: String, position: Null, encoding: BufferEncoding): Promise[BytesWritten]
- def write(data: String, position: Double, encoding: BufferEncoding): Promise[BytesWritten]
- def write(data: String, position: Double): Promise[BytesWritten]
- def write(data: String): Promise[BytesWritten]
- def writeFile(data: Uint8Array, options: BufferEncoding): Promise[Unit]
- def writeFile(data: Uint8Array, options: ObjectEncodingOptions with FlagAndOpenMode with Abortable): Promise[Unit]
- def writeFile(data: Uint8Array): Promise[Unit]
- def writeFile(data: String, options: BufferEncoding): Promise[Unit]
- def writeFile(data: String, options: ObjectEncodingOptions with FlagAndOpenMode with Abortable): Promise[Unit]
- def writeFile(data: String): Promise[Unit]
Asynchronously writes data to a file, replacing the file if it already exists.
data
can be a string, a buffer, an [AsyncIterable](https://tc39.github.io/ecma262/#sec-asynciterable-interface) or [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) object, or an object with an owntoString
function property.Asynchronously writes data to a file, replacing the file if it already exists.
data
can be a string, a buffer, an [AsyncIterable](https://tc39.github.io/ecma262/#sec-asynciterable-interface) or [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) object, or an object with an owntoString
function property. The promise is resolved with no arguments upon success.If
options
is a string, then it specifies theencoding
.The
FileHandle
has to support writing.It is unsafe to use
filehandle.writeFile()
multiple times on the same file without waiting for the promise to be resolved (or rejected).If one or more
filehandle.write()
calls are made on a file handle and then afilehandle.writeFile()
call is made, the data will be written from the current position till the end of the file. It doesn't always write from the beginning of the file.- Since
v10.0.0
- def writev(buffers: Array[ArrayBufferView], position: Double): Promise[WriteVResult]
- def writev(buffers: Array[ArrayBufferView]): Promise[WriteVResult]
Write an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s to the file.
Write an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s to the file.
The promise is resolved with an object containing a two properties:
It is unsafe to call
writev()
multiple times on the same file without waiting for the promise to be resolved (or rejected).On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
- Since
v12.9.0
Deprecated Value Members
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable]) @Deprecated
- Deprecated