# File Management >![](../../public_sys-resources/icon-note.gif) **NOTE:** >The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ``` import fileio from '@ohos.fileio'; ``` ## Required Permissions None ## Usage Before using this module to perform operations on a file or directory, you must obtain the absolute path of the file or directory. For details, see **getOrCreateLocalDir** in the **Context** module. Absolute file or directory path = Application directory path + File name or directory name For example, if the application directory obtained by using the API is _dir_ and the file name is _file name_**.txt**, the absolute path of the file is as follows: ``` let path = dir + "file name.txt" ``` The file descriptor is as follows: ``` let fd = fileio.openSync(path); ``` ## fileio.statSync statSync\(path:string\): Stat Synchronously obtains file information. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the target file.

- Return values

Type

Description

Stat

Detailed file information.

- Example ``` let stat = fileio.statSync(path); ``` ## fileio.opendirSync opendirSync\(path: string\): Dir Synchronously opens a directory. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the directory to open.

- Return values

Type

Description

Dir

An instance of the Dir class corresponding to a directory.

- Example ``` let dir = fileio.opendirSync(path); ``` ## fileio.accessSync accessSync\(path: string, mode?: number\): void Synchronously checks whether the current process can access a file. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the target file.

mode

number

No

Options for accessing the file. You can specify multiple options, separated with a bitwise OR operator (|). The default value is 0.

The options are as follows:

  • 0: Check whether the file exists.
  • 1: Check whether the current process has the execute permission on the file.
  • 2: Check whether the current process has the write permission on the file.
  • 4: Check whether the process has the read permission on the file.
- Example ``` fileio.accessSync(path); ``` ## fileio.closeSync closeSync\(fd: number\): void Synchronously closes a file. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the file to close.

- Example ``` fileio.closeSync(fd); ``` ## fileio.copyFileSync fileio.copyFileSync\(src: string, dest: string, mode?:number\): void Synchronously copies a file. - Parameters

Name

Type

Mandatory

Description

src

string

Yes

Path of the file to copy.

dest

string

Yes

Target file path.

mode

number

No

Option for overwriting the file of the same name in the destination path. The default value is 0, which is the only value supported.

0: Overwrite the file with the same name completely and truncate the part that is not overwritten.

- Example ``` fileio.copyFileSync(src, dest); ``` ## fileio.mkdirSync fileio.mkdirSync\(path: string, mode?: number\): void Synchronously creates a directory. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the directory to create.

mode

number

No

Permission on the directory to create. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is 0o775.

  • 0o775: The owner has the read, write, and execute permissions, and other users have the read and execute permissions on the directory.
  • 0o700: The owner has the read, write, and execute permissions on the directory.
  • 0o400: The owner has the read permission on the directory.
  • 0o200: The owner has the write permission on the directory.
  • 0o100: The owner has the execute permission on the directory.
  • 0o070: The user group has the read, write, and execute permissions on the directory.
  • 0o040: The user group has the read permission on the directory.
  • 0o020: The user group has the write permission on the directory.
  • 0o010: The user group has the execute permission on the directory.
  • 0o007: Other users have the read, write, and execute permissions on the directory.
  • 0o004: Other users have the read permission on the directory.
  • 0o002: Other users have the write permission on the directory.
  • 0o001: Other users have the execute permission on the directory.
- Example ``` fileio.mkdirSync(path); ``` ## fileio.openSync openSync\(path: string, flags?: number, mode?: number\): number Synchronously opens a file. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the file to open.

flags

number

No

Option for opening the file. You must specify one of the following options. By default, the file is open in read-only mode.

  • 0o0: Open file in read-only mode.
  • 0o1: Open file in write-only mode.
  • 0o2: Open file in read/write mode.

In addition, you can specify the following options using a bitwise OR operator (|). By default, no additional option is specified.

  • 0o100: If the file does not exist, create a file. If you use this option, you must also specify mode.
  • 0o200: If 0o100 is added and the file already exists, throw an exception.
  • 0o1000: If the file exists and is open in write-only or read/write mode, truncate the file length to 0.
  • 0o2000: Open the file in append mode. New data will be appended to the file (written to the end of the file).
  • 0o4000: If path points to a named pipe (also known as a FIFO), block special file, or character special file, perform non-blocking operations on the open file and in subsequent I/Os.
  • 0o200000: If path points to a directory, throw an exception.
  • 0o400000: If path points to a symbolic link, throw an exception.
  • 0o4010000: Open the file in synchronous I/O mode.

mode

number

No

Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|). The default value is 0o666.

  • 0o666: The owner, user group, and other users have the read and write permissions on the file.
  • 0o700: The owner has the read, write, and execute permissions on the file.
  • 0o400: The owner has the read permission on the directory.
  • 0o200: The owner has the write permission on the directory.
  • 0o100: The owner has the execute permission on the directory.
  • 0o070: All user groups have the read, write, and execute permissions on the directory.
  • 0o040: All user groups have the read permission on the directory.
  • 0o020: All user groups have the write permission on the directory.
  • 0o010: All user groups have the execute permission on the directory.
  • 0o007: Other users have the read, write, and execute permissions on the directory.
  • 0o004: Other users have the read permission on the directory.
  • 0o002: Other users have the write permission on the directory.
  • 0o001: Other users have the execute permission on the directory.
- Return values

Type

Description

number

File descriptor of the file opened.

- Example ``` fileio.openSync(path); ``` ## fileio.readSync readSync\(fd: number, buffer: ArrayBuffer, options?: Object\): number Synchronously reads data from a file. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the file to read.

buffer

ArrayBuffer

Yes

Buffer used for reading the file.

options

Object

No

The options are as follows:

  • offset (number): position of the buffer to which the data will be read in reference to the start address of the buffer. It is optional. The default value is 0.
  • length (number): length of the data to read. It is optional. The default value is the buffer length minus the offset.
  • position (number): position of the data to read in the file. It is optional. By default, data is read from the current position.
- Return values

Type

Description

number

Length of the data read.

- Example ``` let fd = fileio.openSync(path, 0o2); let buf = new ArrayBuffer(4096); fileio.readSync(fd, buf); console.log(String.fromCharCode.apply(null, new Uint8Array(buf))); ``` ## fileio.rmdirSync rmdirSync\(path: string\): void Synchronously removes a directory. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the directory to remove.

- Example ``` fileio.rmdirSync(path); ``` ## fileio.unlinkSync unlinkSync\(path: string\): void Synchronously deletes a file. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the file to delete.

- Example ``` fileio.unlinkSync(path); ``` ## fileio.writeSync writeSync\(fd: number, buffer: ArrayBuffer | string, options?:Object\): number Synchronously writes data to a file. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the file to write.

buffer

ArrayBuffer | string

Yes

A string or data from a buffer to be written to the file.

options

Object

No

The options are as follows:

  • offset (number): position of the data to write in reference to the start address of the data. It is optional. The default value is 0.
  • length (number): length of the data to write. It is optional. The default value is the buffer length minus the offset.
  • position (number): position of the data to be written in the file. It is optional. By default, data is written from the current position.
  • encoding (string): format of the data to be encoded. You must set it when the data is a string. The default value is utf-8. Only utf-8 is supported.
- Return values

Type

Description

number

Length of the data written in the file.

- Example ``` let fd = fileio.openSync(path, 0o102, 0o666); fileio.writeSync(fd, "hello, world"); ``` ## fileio.chmodSync7+ chmodSync\(path: string, mode: number\): void Synchronously changes the file permissions based on its path. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the target file.

mode

number

Yes

Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).

  • 0o700: The owner has the read, write, and execute permissions on the directory.
  • 0o400: The owner has the read permission on the directory.
  • 0o200: The owner has the write permission on the directory.
  • 0o100: The owner has the execute permission on the directory.
  • 0o070: The user group has the read, write, and execute permissions on the directory.
  • 0o040: The user group has the read permission on the directory.
  • 0o020: The user group has the write permission on the directory.
  • 0o010: The user group has the execute permission on the directory.
  • 0o007: Other users have the read, write, and execute permissions on the directory.
  • 0o004: Other users have the read permission on the directory.
  • 0o002: Other users have the write permission on the directory.
  • 0o001: Other users have the execute permission on the directory.
- Example ``` fileio.chmodSync(fpath, mode); ``` ## fileio.fstatSync7+ fstatSync\(fd: number\): Stat Synchronously obtains file status information based on the file descriptor. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the target file.

- Return values

Type

Description

Promise<Stat>

File status information obtained.

- Example ``` let fd = fileio.openSync(path); let stat = fileio.fstatSync(fd); ``` ## fileio.ftruncateSync7+ ftruncateSync\(fd: number, len?: number\): void Synchronously truncates a file based on the file descriptor. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the file to truncate.

len

number

No

File length, in bytes, after truncation.

- Example ``` fileio.ftruncate(fd, len); ``` ## fileio.fchmodSync7+ fchmodSync\(existingPath: string, newPath: string\): void Synchronously changes the file permissions based on the file descriptor. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the target file.

mode

number

Yes

Permissions on the file. You can specify multiple permissions, separated using a bitwise OR operator (|).

  • 0o700: The owner has the read, write, and execute permissions on the file.
  • 0o400: The owner has the read permission on the directory.
  • 0o200: The owner has the write permission on the directory.
  • 0o100: The owner has the execute permission on the directory.
  • 0o070: The user group has the read, write, and execute permissions on the directory.
  • 0o040: The user group has the read permission on the directory.
  • 0o020: The user group has the write permission on the directory.
  • 0o010: The user group has the execute permission on the directory.
  • 0o007: Other users have the read, write, and execute permissions on the directory.
  • 0o004: Other users have the read permission on the directory.
  • 0o002: Other users have the write permission on the directory.
  • 0o001: Other users have the execute permission on the directory.
- Example ``` fileio.fchmodSync(fd, mode); ``` ## fileio.truncateSync7+ truncateSync\(fpath: string, len?: number\): void Synchronously truncates a file based on the file path. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the file to truncate.

len

number

No

File length, in bytes, after truncation.

- Example ``` fileio.ftruncate(path, len); ``` ## fileio.renameSync7+ renameSync\(oldPath: string, newPath: string\): void Synchronously renames a file. - Parameters

Name

Type

Mandatory

Description

oldPath

string

Yes

Absolute path of the file to rename.

Newpath

String

Yes

Absolute path of the file renamed.

- Example ``` fileio.renameSync(oldpath, newpath); ``` ## fileio.fsyncSync7+ fsyncSync\(fd: number\): void Synchronizes a file in synchronous mode. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the file to synchronize.

- Example ``` fileio.fyncsSync(fd); ``` ## fileio.chownSync7+ chownSync\(path: string, uid: number, gid: number\): void Synchronously changes the file owner based on its path. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the target file.

uid

number

Yes

New UID.

gid

number

Yes

New GID.

- Example ``` let stat = fileio.statSync(fpath) fileio.chownSync(path, stat.uid, stat.gid); ``` ## fileio.createStreamSync7+ createStreamSync\(path: string, mode: string\): Stream Synchronously opens a stream based on the file path. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Absolute path of the target file.

mode

string

Yes

  • r: Open a file for reading. The file must exist.
  • r+: Open a file for both reading and writing. The file must exist.
  • w: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
  • w+: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
  • a: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
  • a+: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- Return values

Name

Description

Stream

Stream operation result.

- Example ``` let ss = fileio.createStream(path, "r+"); ``` ## fileio.fdopenStreamSync7+ fdopenStreamSync\(fd: number, mode: string\): Stream Synchronously opens a stream based on the file descriptor. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the target file.

mode

string

Yes

  • r: Open a file for reading. The file must exist.
  • r+: Open a file for both reading and writing. The file must exist.
  • w: Open a file for writing. If the file exists, clear its content. If the file does not exist, create a file.
  • w+: Open a file for both reading and writing. If the file exists, clear its content. If the file does not exist, create a file.
  • a: Open a file in append mode for writing at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
  • a+: Open a file in append mode for reading or updating at the end of the file. If the file does not exist, create a file. If the file exists, write data to the end of the file (the original content of the file is reserved).
- Return values

Name

Description

Stream

Stream operation result.

- Example ``` let ss = fileio.fdopenStreamSync(fd, "r+"); ``` ## fileio.fchownSync7+ fchownSync\(fd: number, uid: number, gid: number\): void Synchronously changes the file owner based on the file descriptor. - Parameters

Name

Type

Mandatory

Description

fd

number

Yes

File descriptor of the target file.

uid

number

Yes

New UID.

gid

number

Yes

New GID.

- Example ``` let stat = fileio.statSync(fpath); fileio.fchownSync(fd, stat.uid, stat.gid); ``` ## Stat Provides detailed file information. Before invoking a method of the **Stat** class, use the [fileio.statSync](#section014281412198) method to create a **Stat** instance. ### Attributes

Name

Type

Readable

Writable

Description

dev

number

Yes

No

Major device number.

ino

number

Yes

No

File ID. Different files on the same device have different inos.

mode

number

Yes

No

File type and permission. The first four bits indicate the file type, and the last 12 bits indicate the permission. The bit fields are described follows:

  • 0o170000: mask used to obtain the file type.
  • 0o140000: The file is a socket.
  • 0o120000: The file is a symbolic link.
  • 0o100000: The file is a regular file.
  • 0o060000: The file is a block device.
  • 0o040000: The file is a directory.
  • 0o020000: The file is a character device.
  • 0o010000: The file is a named pipe (also known as a FIFO).
  • 0o0700: mask used to obtain owner permissions.
  • 0o0400: The owner has the read permission on a regular file or a directory entry.
  • 0o0200: The owner has the permission to write a regular file or has the permission to create and delete a directory entry.
  • 0o0100: The owner has the permission to execute a regular file or has the permission to search for the specified path in a directory.
  • 0o0070: mask used to obtain user group permissions.
  • 0o0040: The user group has the read permission on a regular file or a directory entry.
  • 0o0020: The user group has the permission to write a regular file or has the permission to create and delete a directory entry.
  • 0o0010: The user group has the permission to execute a regular file or has the permission to search for the specified path in a directory.
  • 0o0007: mask used to obtain permissions of other users.
  • 0o0004: Other user groups have the read permission on a regular file or a directory entry.
  • 0o0002: Other user groups have the permission to write a regular file or have the permission to create and delete a directory entry.
  • 0o0001: Other user groups have the permission to execute a regular file or have the permission to search for the specified path in a directory.

nlink

number

Yes

No

Number of hard links in the file.

uid

number

Yes

No

ID of the file owner.

gid

number

Yes

No

ID of the user group of the file.

rdev

number

Yes

No

Minor device number.

size

number

Yes

No

File size, in bytes. This parameter is valid only for regular files.

blocks

number

Yes

No

Number of blocks occupied by a file. Each block is 512 bytes.

atime

number

Yes

No

Time of the last access of file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.

mtime

number

Yes

No

Time of the last modification of the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.

ctime

number

Yes

No

Time of the last status change of the file. The value is the number of seconds elapsed since 00:00:00 on January 1, 1970.

### isBlockDevice isBlockDevice\(\): boolean Checks whether a directory entry is a block special file. A block special file supports access by block only, and it is cached when accessed. - Return values

Type

Description

boolean

Whether the directory entry is a block special file.

- Example ``` let isBLockDevice = fileio.statSync(path).isBlockDevice(); ``` ### isCharacterDevice isCharacterDevice\(\): boolean Checks whether a directory entry is a character special file. A character special file supports random access, and it is not cached when accessed. - Return values

Type

Description

boolean

Whether the directory entry is a character special file.

- Example ``` let isCharacterDevice = fileio.statSync(path).isCharacterDevice(); ``` ### isDirectory isDirectory\(\): boolean Checks whether a directory entry is a directory. - Return values

Type

Description

boolean

Whether the directory entry is a directory.

- Example ``` let isDirectory= fileio.statSync(path).isDirectory(); ``` ### isFIFO isFIFO\(\): boolean Checks whether a directory entry is a named pipe \(or FIFO\). Named pipes are used for inter-process communication. - Return values

Type

Description

boolean

Whether the directory entry is a FIFO.

- Example ``` let isFIFO= fileio.statSync(path).isFIFO(); ``` ### isFile isFile\(\): boolean Checks whether a directory entry is a regular file. - Return values

Type

Description

boolean

Whether the directory entry is a regular file.

- Example ``` let isFile= fileio.statSync(fpath).isFile(); ``` ### isSocket isSocket\(\): boolean Checks whether a directory entry is a socket. - Return values

Type

Description

boolean

Whether the directory entry is a socket.

- Example ``` let isSocket = fileio.statSync(path).isSocket(); ``` ### isSymbolicLink isSymbolicLink\(\): boolean Checks whether a directory entry is a symbolic link. - Return values

Type

Description

boolean

Whether the directory entry is a symbolic link.

- Example ``` let isSymbolicLink = fileio.statSync(path).isSymbolicLink(); ``` ## Stream7+ File stream. Before calling a method of the **Stream** class, use the [fileio.createStreamSync](#section1956102153713) method to create a **Stream** instance. ### closeSync7+ closeSync\(\): void Synchronously closes the stream. - Example ``` let ss= fileio.createStreamSync(path); ss.closeSync(); ``` ### flushSync7+ flushSync\(\): void Synchronously flushes the stream. - Example ``` let ss= fileio.createStreamSync(path); ss.flushSync(); ``` ### writeSync7+ writeSync\(buffer: ArrayBuffer | string, options?:Object\): number Synchronously writes data into the stream. - Parameters

Name

Type

Mandatory

Description

buffer

ArrayBuffer | string

Yes

A string or data from a buffer to be written to the file.

options

Object

No

The options are as follows:

  • offset (number): position of the data to write in reference to the start address of the data. It is optional. The default value is 0.
  • length (number): length of the data to write. It is optional. The default value is the buffer length minus the offset.
  • position (number): position of the data to be written in the file. It is optional. By default, data is written from the current position.
  • encoding (string): format of the data to be encoded. You must set it when the data is a string. The default value is utf-8. Only utf-8 is supported.
- Return values

Type

Description

number

Length of the data written in the file.

- Example ``` let ss= fileio.createStreamSync(fpath,"r+"); ss.writeSync("hello, world",{offset: 1,length: 5,position: 5,encoding :'utf-8'}); ``` ### readSync7+ readSync\(buffer: ArrayBuffer, options?: Object\): number Synchronously reads data from the stream. - Parameters

Name

Type

Mandatory

Description

buffer

ArrayBuffer

Yes

Buffer used for reading the file.

options

Object

No

The options are as follows:

  • offset (number): position of the buffer to which the data will be read in reference to the start address of the buffer. It is optional. The default value is 0.
  • length (number): length of the data to read. It is optional. The default value is the buffer length minus the offset.
  • position (number): position of the data to read in the file. It is optional. By default, data is read from the current position.
- Return values

Type

Description

number

Length of the data read.

- Example ``` let ss = fileio.createStreamSync(fpath, "r+"); ss.readSync(new ArrayBuffer(4096),{offset: 1,length: FILE_CONTENT.length,position: 5}); ``` ## Dir Manages directories. Before calling a method of the **Dir** class, use the [fileio.opendirSync](#section7741145112216) method to create a **Dir** instance. ### readSync readSync\(\): Dirent Synchronously reads the next directory entry. - Return values

Type

Description

Dirent

Directory entry read.

- Example ``` let dir = fileio.opendirSync(dpath); let dirent = dir.readSync(); console.log(dirent.name); ``` ### closeSync closeSync\(\): void Closes a directory. After a directory is closed, the file descriptor in **Dir** will be released and the directory entry cannot be read from **Dir**. - Example ``` let dir = fileio.opendirSync(dpath); dir.closeSync(); ``` ## Dirent Provides information about files and directories. Before calling a method of the **Dirent** class, use the [readSync](#section10198204912710) method to create a **Dirent** instance. ### Attributes

Name

Type

Readable

Writable

Description

name

string

Yes

No

Directory entry name.

### isBlockDevice isBlockDevice\(\): boolean Checks whether a directory entry is a block device file. A block special file supports access by block only, and it is cached when accessed. - Return values

Type

Description

boolean

Whether the directory entry is a block device file.

- Example ``` let dir = fileio.opendirSync(dpath); let isBLockDevice = dir.readSync().isBlockDevice(); ``` ### isCharacterDevice isCharacterDevice\(\): boolean Checks whether a directory entry is a character device file. A character special file supports random access, and it is not cached when accessed. - Return values

Type

Description

boolean

Whether the directory entry is a character device file.

- Example ``` let dir = fileio.opendirSync(dpath); let isCharacterDevice = dir.readSync().isCharacterDevice(); ``` ### isDirectory isDirectory\(\): boolean Checks whether a directory entry is a directory. - Return values

Type

Description

boolean

Whether the directory entry is a directory.

- Example ``` let dir = fileio.opendirSync(dpath); let isDirectory = dir.readSync().isDirectory(); ``` ### isFIFO isFIFO\(\): boolean Checks whether a directory entry is a named pipe \(or FIFO\). Named pipes are used for inter-process communication. - Return values

Type

Description

boolean

Whether the directory entry is a FIFO.

- Example ``` let dir = fileio.opendirSync(dpath); let isFIFO = dir.readSync().isFIFO(); ``` ### isFile isFile\(\): boolean Checks whether a directory entry is a regular file. - Return values

Type

Description

boolean

Whether the directory entry is a regular file.

- Example ``` let dir = fileio.opendirSync(dpath); let isFile = dir.readSync().isFile(); ``` ### isSocket isSocket\(\): boolean Checks whether a directory entry is a socket. - Return values

Type

Description

boolean

Whether the directory entry is a socket.

- Example ``` let dir = fileio.opendirSync(dpath); let isSocket = dir.readSync().isSocket(); ``` ### isSymbolicLink isSymbolicLink\(\): boolean Checks whether a directory entry is a symbolic link. - Return values

Type

Description

boolean

Whether the directory entry is a symbolic link.

- Example ``` let dir = fileio.opendirSync(dpath); let isSymbolicLink = dir.readSync().isSymbolicLink(); ```