温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/reference/node/fs/readFile
点击访问原文链接

Node fs.readFile function | API Reference | Bun

Node fs.readFile function | API Reference | BunBuildDocsReferenceGuidesBlogDiscord/node:fs/readFileNreadFile

Search the reference...

/

BuildDocsReferenceGuidesBlogDiscord/node:fs/readFileNreadFile

namespace

fs.readFile { if (err) throw err; console.log(data); }); ``` The callback is passed two arguments `(err, data)`, where `data` is the contents of the file. If no encoding is specified, then the raw buffer is returned. If `options` is a string, then it specifies the encoding: ```js import { readFile } from 'node:fs'; readFile('/etc/passwd', 'utf8', callback); ``` When the path is a directory, the behavior of `fs.readFile()` and readFileSync is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned. ```js import { readFile } from 'node:fs'; // macOS, Linux, and Windows readFile('', (err, data) => { // => [Error: EISDIR: illegal operation on a directory, read ] }); // FreeBSD readFile('', (err, data) => { // => null, }); ``` It is possible to abort an ongoing request using an `AbortSignal`. If a request is aborted the callback is called with an `AbortError`: ```js import { readFile } from 'node:fs'; const controller = new AbortController(); const signal = controller.signal; readFile(fileInfo[0].name, { signal }, (err, buf) => { // ... }); // When you want to abort the request controller.abort(); ``` The `fs.readFile()` function buffers the entire file. To minimize memory costs, when possible prefer streaming via `fs.createReadStream()`. Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering `fs.readFile` performs." data-algolia-static="false" data-algolia-merged="true" data-type="Namespace">function readFile(path: PathOrFileDescriptor,options: undefined | null | { encoding: null; flag: string } & Abortable,callback: (err: null | ErrnoException, data: NonSharedBuffer) => void): void;

Asynchronously reads the entire contents of a file.

import { readFile } from 'node:fs';

readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});

The callback is passed two arguments (err, data), where data is the contents of the file.

If no encoding is specified, then the raw buffer is returned.

If options is a string, then it specifies the encoding:

import { readFile } from 'node:fs';

readFile('/etc/passwd', 'utf8', callback);

When the path is a directory, the behavior of fs.readFile() and readFileSync is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned.

import { readFile } from 'node:fs';

// macOS, Linux, and Windows
readFile('', (err, data) => {
// => [Error: EISDIR: illegal operation on a directory, read ]
});

// FreeBSD
readFile('', (err, data) => {
// => null,
});

It is possible to abort an ongoing request using an AbortSignal. If a request is aborted the callback is called with an AbortError:

import { readFile } from 'node:fs';

const controller = new AbortController();
const signal = controller.signal;
readFile(fileInfo[0].name, { signal }, (err, buf) => {
// ...
});
// When you want to abort the request
controller.abort();

The fs.readFile() function buffers the entire file. To minimize memory costs, when possible prefer streaming via fs.createReadStream().

Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering fs.readFile performs.

@param path

filename or file descriptor

function readFile(path: PathOrFileDescriptor,options: BufferEncoding | { encoding: BufferEncoding; flag: string } & Abortable,callback: (err: null | ErrnoException, data: string) => void): void;

Asynchronously reads the entire contents of a file.

@param path

A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

@param options

Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

function readFile(path: PathOrFileDescriptor,options: undefined | null | BufferEncoding | ObjectEncodingOptions & { flag: string } & Abortable,callback: (err: null | ErrnoException, data: string | NonSharedBuffer) => void): void;

Asynchronously reads the entire contents of a file.

@param path

A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

@param options

Either the encoding for the result, or an object that contains the encoding and an optional flag. If a flag is not provided, it defaults to 'r'.

function readFile(path: PathOrFileDescriptor,callback: (err: null | ErrnoException, data: NonSharedBuffer) => void): void;

Asynchronously reads the entire contents of a file.

@param path

A path to a file. If a URL is provided, it must use the file: protocol. If a file descriptor is provided, the underlying file will not be closed automatically.

namespace readFile

Resources

ReferenceDocsGuidesDiscordMerch StoreGitHubBlog 

Toolkit

RuntimePackage managerTest runnerBundlerPackage runner

Project

Bun 1.0Bun 1.1Bun 1.2Bun 1.3RoadmapContributingLicense

Baked with ❤️ in San Francisco

We're hiring →

Node fs.readFile function | API Reference | Bun,AI智能索引,全网链接索引,智能导航,网页索引

    API documentation for namespace node:fs.readFile | Bun