文件管理

2024-01-23 16:31 更新

該模塊為基礎文件操作API,提供基礎文件操作能力,包括文件基本管理、文件目錄管理、文件信息統(tǒng)計、文件流式讀寫等常用功能。

說明

本模塊首批接口從API version 9開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

導入模塊

  1. import fs from '@ohos.file.fs';

使用說明

使用該功能模塊對文件/目錄進行操作前,需要先獲取其應用沙箱路徑,獲取方式及其接口用法請參考:

Stage模型

  1. import UIAbility from '@ohos.app.ability.UIAbility';
  2. export default class EntryAbility extends UIAbility {
  3. onWindowStageCreate(windowStage) {
  4. let context = this.context;
  5. let pathDir = context.filesDir;
  6. }
  7. }

FA模型

  1. import featureAbility from '@ohos.ability.featureAbility';
  2. let context = featureAbility.getContext();
  3. context.getFilesDir().then((data) => {
  4. let pathDir = data;
  5. })

FA模型context的具體獲取方法參見FA模型。

fs.stat

stat(file: string|number): Promise<Stat>

獲取文件詳細屬性信息,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

string|number

文件應用沙箱路徑path或已打開的文件描述符fd。

返回值:

類型

說明

Promise<Stat>

Promise對象。返回文件的具體信息。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.stat(filePath).then((stat) => {
  3. console.info("get file info succeed, the size of file is " + stat.size);
  4. }).catch((err) => {
  5. console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
  6. });

fs.stat

stat(file: string|number, callback: AsyncCallback<Stat>): void

獲取文件詳細屬性信息,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

string|number

文件應用沙箱路徑path或已打開的文件描述符fd。

callback

AsyncCallback<Stat>

異步獲取文件的信息之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. fs.stat(pathDir, (err, stat) => {
  2. if (err) {
  3. console.error("get file info failed with error message: " + err.message + ", error code: " + err.code);
  4. } else {
  5. console.info("get file info succeed, the size of file is " + stat.size);
  6. }
  7. });

fs.statSync

statSync(file: string|number): Stat

以同步方法獲取文件詳細屬性信息。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

string|number

文件應用沙箱路徑path或已打開的文件描述符fd。

返回值:

類型

說明

Stat

表示文件的具體信息。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let stat = fs.statSync(pathDir);
  2. console.info("get file info succeed, the size of file is " + stat.size);

fs.access

access(path: string): Promise<boolean>

檢查文件是否存在,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件應用沙箱路徑。

返回值:

類型

說明

Promise<boolean>

Promise對象。返回boolean,表示文件是否存在。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.access(filePath).then((res) => {
  3. if (res) {
  4. console.info("file exists");
  5. }
  6. }).catch((err) => {
  7. console.error("access failed with error message: " + err.message + ", error code: " + err.code);
  8. });

fs.access

access(path: string, callback: AsyncCallback<boolean>): void

檢查文件是否存在,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件應用沙箱路徑。

callback

AsyncCallback<boolean>

異步檢查文件是否存在的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.access(filePath, (err, res) => {
  3. if (err) {
  4. console.error("access failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. if (res) {
  7. console.info("file exists");
  8. }
  9. }
  10. });

fs.accessSync

accessSync(path: string): boolean

以同步方法檢查文件是否存在。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件應用沙箱路徑。

返回值:

類型

說明

boolean

返回boolean,表示文件是否存在。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. try {
  3. let res = fs.accessSync(filePath);
  4. if (res) {
  5. console.info("file exists");
  6. }
  7. } catch(err) {
  8. console.error("accessSync failed with error message: " + err.message + ", error code: " + err.code);
  9. }

fs.close

close(file: File|number): Promise<void>

關閉文件,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

File|number

已打開的File對象或已打開的文件描述符fd。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.close(file).then(() => {
  4. console.info("close file succeed");
  5. }).catch((err) => {
  6. console.error("close file failed with error message: " + err.message + ", error code: " + err.code);
  7. });

fs.close

close(file: File|number, callback: AsyncCallback<void>): void

關閉文件,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

File|number

已打開的File對象或已打開的文件描述符fd。

callback

AsyncCallback<void>

異步關閉文件之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.close(file, (err) => {
  4. if (err) {
  5. console.error("close file failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("close file success");
  8. }
  9. });

fs.closeSync

closeSync(file: File|number): void

以同步方法關閉文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

File|number

已打開的File對象或已打開的文件描述符fd。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.closeSync(file);

fs.copyFile

copyFile(src: string|number, dest: string|number, mode?: number): Promise<void>

復制文件,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

src

string|number

待復制文件的路徑或待復制文件的文件描述符。

dest

string|number

目標文件路徑或目標文件的文件描述符。

mode

number

mode提供覆蓋文件的選項,當前僅支持0,且默認為0。

0:完全覆蓋目標文件。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcPath = pathDir + "/srcDir/test.txt";
  2. let dstPath = pathDir + "/dstDir/test.txt";
  3. fs.copyFile(srcPath, dstPath).then(() => {
  4. console.info("copy file succeed");
  5. }).catch((err) => {
  6. console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
  7. });

fs.copyFile

copyFile(src: string|number, dest: string|number, mode?: number, callback: AsyncCallback<void>): void

復制文件,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

src

string|number

待復制文件的路徑或待復制文件的文件描述符。

dest

string|number

目標文件路徑或目標文件的文件描述符。

mode

number

mode提供覆蓋文件的選項,當前僅支持0,且默認為0。

0:完全覆蓋目標文件,未覆蓋部分將被裁切掉。

callback

AsyncCallback<void>

異步復制文件之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let srcPath = pathDir + "/srcDir/test.txt";
  2. let dstPath = pathDir + "/dstDir/test.txt";
  3. fs.copyFile(srcPath, dstPath, (err) => {
  4. if (err) {
  5. console.error("copy file failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("copy file success");
  8. }
  9. });

fs.copyFileSync

copyFileSync(src: string|number, dest: string|number, mode?: number): void

以同步方法復制文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

src

string|number

待復制文件的路徑或待復制文件的文件描述符。

dest

string|number

目標文件路徑或目標文件的文件描述符。

mode

number

mode提供覆蓋文件的選項,當前僅支持0,且默認為0。

0:完全覆蓋目標文件,未覆蓋部分將被裁切掉。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcPath = pathDir + "/srcDir/test.txt";
  2. let dstPath = pathDir + "/dstDir/test.txt";
  3. fs.copyFileSync(srcPath, dstPath);

fs.mkdir

mkdir(path: string): Promise<void>

創(chuàng)建目錄,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

目錄的應用沙箱路徑。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let dirPath = pathDir + "/testDir";
  2. fs.mkdir(dirPath).then(() => {
  3. console.info("mkdir succeed");
  4. }).catch((err) => {
  5. console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
  6. });

fs.mkdir

mkdir(path: string, callback: AsyncCallback<void>): void

創(chuàng)建目錄,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

目錄的應用沙箱路徑。

callback

AsyncCallback<void>

異步創(chuàng)建目錄操作完成之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let dirPath = pathDir + "/testDir";
  2. fs.mkdir(dirPath, (err) => {
  3. if (err) {
  4. console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.info("mkdir success");
  7. }
  8. });

fs.mkdirSync

mkdirSync(path: string): void

以同步方法創(chuàng)建目錄。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

目錄的應用沙箱路徑。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let dirPath = pathDir + "/testDir";
  2. fs.mkdirSync(dirPath);

fs.open

open(path: string, mode?: number): Promise<File>

打開文件,使用Promise異步回調(diào)。支持使用URI打開文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑或文件URI。

mode

number

打開文件的選項,必須指定如下選項中的一個,默認以只讀方式打開:

- OpenMode.READ_ONLY(0o0):只讀打開。

- OpenMode.WRITE_ONLY(0o1):只寫打開。

- OpenMode.READ_WRITE(0o2):讀寫打開。

給定如下功能選項,以按位或的方式追加,默認不給定任何額外選項:

- OpenMode.CREATE(0o100):若文件不存在,則創(chuàng)建文件。

- OpenMode.TRUNC(0o1000):如果文件存在且以只寫或讀寫的方式打開文件,則將其長度裁剪為零。

- OpenMode.APPEND(0o2000):以追加方式打開,后續(xù)寫將追加到文件末尾。

- OpenMode.NONBLOCK(0o4000):如果path指向FIFO、塊特殊文件或字符特殊文件,則本次打開及后續(xù) IO 進行非阻塞操作。

- OpenMode.DIR(0o200000):如果path不指向目錄,則出錯。

- OpenMode.NOFOLLOW(0o400000):如果path指向符號鏈接,則出錯。

- OpenMode.SYNC(0o4010000):以同步IO的方式打開文件。

返回值:

類型

說明

Promise<File>

Promise對象。返回File對象。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).then((file) => {
  3. console.info("file fd: " + file.fd);
  4. }).catch((err) => {
  5. console.error("open file failed with error message: " + err.message + ", error code: " + err.code);
  6. });

fs.open

open(path: string, mode?: number, callback: AsyncCallback<File>): void

打開文件,使用callback異步回調(diào)。支持使用URI打開文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑或URI。

mode

number

打開文件的選項,必須指定如下選項中的一個,默認以只讀方式打開:

- OpenMode.READ_ONLY(0o0):只讀打開。

- OpenMode.WRITE_ONLY(0o1):只寫打開。

- OpenMode.READ_WRITE(0o2):讀寫打開。

給定如下功能選項,以按位或的方式追加,默認不給定任何額外選項:

- OpenMode.CREATE(0o100):若文件不存在,則創(chuàng)建文件。

- OpenMode.TRUNC(0o1000):如果文件存在且以只寫或讀寫的方式打開文件,則將其長度裁剪為零。

- OpenMode.APPEND(0o2000):以追加方式打開,后續(xù)寫將追加到文件末尾。

- OpenMode.NONBLOCK(0o4000):如果path指向FIFO、塊特殊文件或字符特殊文件,則本次打開及后續(xù) IO 進行非阻塞操作。

- OpenMode.DIR(0o200000):如果path不指向目錄,則出錯。

- OpenMode.NOFOLLOW(0o400000):如果path指向符號鏈接,則出錯。

- OpenMode.SYNC(0o4010000):以同步IO的方式打開文件。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE, (err, file) => {
  3. if (err) {
  4. console.error("mkdir failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.info("file fd: " + file.fd);
  7. }
  8. });

fs.openSync

openSync(path: string, mode?: number): File

以同步方法打開文件。支持使用URI打開文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

打開文件的應用沙箱路徑或URI。

mode

number

打開文件的選項,必須指定如下選項中的一個,默認以只讀方式打開:

- OpenMode.READ_ONLY(0o0):只讀打開。

- OpenMode.WRITE_ONLY(0o1):只寫打開。

- OpenMode.READ_WRITE(0o2):讀寫打開。

給定如下功能選項,以按位或的方式追加,默認不給定任何額外選項:

- OpenMode.CREATE(0o100):若文件不存在,則創(chuàng)建文件。

- OpenMode.TRUNC(0o1000):如果文件存在且以只寫或讀寫的方式打開文件,則將其長度裁剪為零。

- OpenMode.APPEND(0o2000):以追加方式打開,后續(xù)寫將追加到文件末尾。

- OpenMode.NONBLOCK(0o4000):如果path指向FIFO、塊特殊文件或字符特殊文件,則本次打開及后續(xù) IO 進行非阻塞操作。

- OpenMode.DIR(0o200000):如果path不指向目錄,則出錯。

- OpenMode.NOFOLLOW(0o400000):如果path指向符號鏈接,則出錯。

- OpenMode.SYNC(0o4010000):以同步IO的方式打開文件。

返回值:

類型

說明

File

打開的File對象。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  3. console.info("file fd: " + file.fd);
  4. fs.closeSync(file);

fs.read

read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): Promise<number>

從文件讀取數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

buffer

ArrayBuffer

用于保存讀取到的文件數(shù)據(jù)的緩沖區(qū)。

options

Object

支持如下選項:

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀。

- length,number類型,表示期望讀取數(shù)據(jù)的長度。可選,默認緩沖區(qū)長度。

返回值:

類型

說明

Promise<number>

Promise對象。返回讀取的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
  3. let buf = new ArrayBuffer(4096);
  4. fs.read(file.fd, buf).then((readLen) => {
  5. console.info("read file data succeed");
  6. console.info(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen))));
  7. fs.closeSync(file);
  8. }).catch((err) => {
  9. console.error("read file data failed with error message: " + err.message + ", error code: " + err.code);
  10. });

fs.read

read(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }, callback: AsyncCallback<number>): void

從文件讀取數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

buffer

ArrayBuffer

用于保存讀取到的文件數(shù)據(jù)的緩沖區(qū)。

options

Object

支持如下選項:

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀。

- length,number類型,表示期望讀取數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

callback

AsyncCallback<number>

異步讀取數(shù)據(jù)之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
  3. let buf = new ArrayBuffer(4096);
  4. fs.read(file.fd, buf, (err, readLen) => {
  5. if (err) {
  6. console.error("read failed with error message: " + err.message + ", error code: " + err.code);
  7. } else {
  8. console.info("read file data succeed");
  9. console.info(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen))));
  10. fs.closeSync(file);
  11. }
  12. });

fs.readSync

readSync(fd: number, buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number

以同步方法從文件讀取數(shù)據(jù)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

buffer

ArrayBuffer

用于保存讀取到的文件數(shù)據(jù)的緩沖區(qū)。

options

Object

支持如下選項:

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀。

- length,number類型,表示期望讀取數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

返回值:

類型

說明

number

實際讀取的長度。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
  3. let buf = new ArrayBuffer(4096);
  4. let num = fs.readSync(file.fd, buf);
  5. fs.closeSync(file);

fs.rmdir

rmdir(path: string): Promise<void>

刪除整個目錄,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

目錄的應用沙箱路徑。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let dirPath = pathDir + "/testDir";
  2. fs.rmdir(dirPath).then(() => {
  3. console.info("rmdir succeed");
  4. }).catch((err) => {
  5. console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code);
  6. });

fs.rmdir

rmdir(path: string, callback: AsyncCallback<void>): void

刪除整個目錄,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

目錄的應用沙箱路徑。

callback

AsyncCallback<void>

異步刪除目錄之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let dirPath = pathDir + "/testDir";
  2. fs.rmdir(dirPath, (err) => {
  3. if (err) {
  4. console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.info("rmdir succeed");
  7. }
  8. });

fs.rmdirSync

rmdirSync(path: string): void

以同步方法刪除目錄。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

目錄的應用沙箱路徑。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let dirPath = pathDir + "/testDir";
  2. fs.rmdirSync(dirPath);

fs.unlink

unlink(path: string): Promise<void>

刪除單個文件,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.unlink(filePath).then(() => {
  3. console.info("remove file succeed");
  4. }).catch((err) => {
  5. console.error("remove file failed with error message: " + err.message + ", error code: " + err.codeor);
  6. });

fs.unlink

unlink(path: string, callback: AsyncCallback<void>): void

刪除文件,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

callback

AsyncCallback<void>

異步刪除文件之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.unlink(filePath, (err) => {
  3. if (err) {
  4. console.error("remove file failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.info("remove file succeed");
  7. }
  8. });

fs.unlinkSync

unlinkSync(path: string): void

以同步方法刪除文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.unlinkSync(filePath);

fs.write

write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): Promise<number>

將數(shù)據(jù)寫入文件,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

buffer

ArrayBuffer|string

待寫入文件的數(shù)據(jù),可來自緩沖區(qū)或字符串。

options

Object

支持如下選項:

- offset,number類型,表示期望寫入文件的位置??蛇x,默認從當前位置開始寫。

- length,number類型,表示期望寫入數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

- encoding,string類型,當數(shù)據(jù)是string類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8'。當前僅支持 'utf-8'。

返回值:

類型

說明

Promise<number>

Promise對象。返回實際寫入的長度。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  3. fs.write(file.fd, "hello, world").then((writeLen) => {
  4. console.info("write data to file succeed and size is:" + writeLen);
  5. fs.closeSync(file);
  6. }).catch((err) => {
  7. console.error("write data to file failed with error message: " + err.message + ", error code: " + err.code);
  8. });

fs.write

write(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<number>): void

將數(shù)據(jù)寫入文件,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

buffer

ArrayBuffer|string

待寫入文件的數(shù)據(jù),可來自緩沖區(qū)或字符串。

options

Object

支持如下選項:

- offset,number類型,表示期望寫入文件的位置。可選,默認從當前位置開始寫。

- length,number類型,表示期望寫入數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

- encoding,string類型,當數(shù)據(jù)是string類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8'。當前僅支持 'utf-8'。

callback

AsyncCallback<number>

異步將數(shù)據(jù)寫入完成后執(zhí)行的回調(diào)函數(shù)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  3. fs.write(file.fd, "hello, world", (err, writeLen) => {
  4. if (err) {
  5. console.error("write failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("write data to file succeed and size is:" + writeLen);
  8. fs.closeSync(file);
  9. }
  10. });

fs.writeSync

writeSync(fd: number, buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): number

以同步方法將數(shù)據(jù)寫入文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

buffer

ArrayBuffer|string

待寫入文件的數(shù)據(jù),可來自緩沖區(qū)或字符串。

options

Object

支持如下選項:

- offset,number類型,表示期望寫入文件的位置??蛇x,默認從當前位置開始寫。

- length,number類型,表示期望寫入數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

- encoding,string類型,當數(shù)據(jù)是string類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8'。當前僅支持 'utf-8'。

返回值:

類型

說明

number

實際寫入的長度。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  3. let writeLen = fs.writeSync(file.fd, "hello, world");
  4. console.info("write data to file succeed and size is:" + writeLen);
  5. fs.closeSync(file);

fs.truncate

truncate(file: string|number, len?: number): Promise<void>

截斷文件,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

string|number

文件的應用沙箱路徑或已打開的文件描述符fd。

len

number

文件截斷后的長度,以字節(jié)為單位。默認為0。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let len = 5;
  3. fs.truncate(filePath, len).then(() => {
  4. console.info("truncate file succeed");
  5. }).catch((err) => {
  6. console.error("truncate file failed with error message: " + err.message + ", error code: " + err.code);
  7. });

fs.truncate

truncate(file: string|number, len?: number, callback: AsyncCallback<void>): void

截斷文件,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

string|number

文件的應用沙箱路徑或已打開的文件描述符fd。

len

number

文件截斷后的長度,以字節(jié)為單位。默認為0。

callback

AsyncCallback<void>

回調(diào)函數(shù),本調(diào)用無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let len = 5;
  3. fs.truncate(filePath, len, (err) => {
  4. if (err) {
  5. console.error("truncate failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("truncate success");
  8. }
  9. });

fs.truncateSync

truncateSync(file: string|number, len?: number): void

以同步方法截斷文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

file

string|number

文件的應用沙箱路徑或已打開的文件描述符fd。

len

number

文件截斷后的長度,以字節(jié)為單位。默認為0。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let len = 5;
  3. fs.truncateSync(filePath, len);

fs.readText

readText(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }): Promise<string>

基于文本方式讀取文件(即直接讀取文件的文本內(nèi)容),使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

filePath

string

文件的應用沙箱路徑。

options

Object

支持如下選項:

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀取。

- length,number類型,表示期望讀取數(shù)據(jù)的長度??蛇x,默認文件長度。

- encoding,string類型,當數(shù)據(jù)是 string 類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8',僅支持 'utf-8'。

返回值:

類型

說明

Promise<string>

Promise對象。返回讀取文件的內(nèi)容。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.readText(filePath).then((str) => {
  3. console.info("readText succeed:" + str);
  4. }).catch((err) => {
  5. console.error("readText failed with error message: " + err.message + ", error code: " + err.code);
  6. });

fs.readText

readText(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<string>): void

基于文本方式讀取文件(即直接讀取文件的文本內(nèi)容),使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

filePath

string

文件的應用沙箱路徑。

options

Object

支持如下選項:

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀取。

- length,number類型,表示期望讀取數(shù)據(jù)的長度??蛇x,默認文件長度。

- encoding,string類型,表示數(shù)據(jù)的編碼方式,默認 'utf-8',僅支持 'utf-8'。

callback

AsyncCallback<string>

回調(diào)函數(shù),返回讀取文件的內(nèi)容。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.readText(filePath, { offset: 1, encoding: 'utf-8' }, (err, str) => {
  3. if (err) {
  4. console.error("read text failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.info("readText succeed:" + str);
  7. }
  8. });

fs.readTextSync

readTextSync(filePath: string, options?: { offset?: number; length?: number; encoding?: string; }): string

以同步方法基于文本方式讀取文件(即直接讀取文件的文本內(nèi)容)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

filePath

string

文件的應用沙箱路徑。

options

Object

支持如下選項:

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀取。

- length,number類型,表示期望讀取數(shù)據(jù)的長度??蛇x,默認文件長度。

- encoding,string類型,當數(shù)據(jù)是 string 類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8',僅支持 'utf-8'。

返回值:

類型

說明

string

返回讀取文件的內(nèi)容。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let str = fs.readTextSync(filePath, {offset: 1, length: 3});
  3. console.info("readText succeed:" + str);

fs.lstat

lstat(path: string): Promise<Stat>

獲取鏈接文件信息,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

返回值:

類型

說明

Promise<Stat>

promise對象,返回文件對象,表示文件的具體信息,詳情見stat。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.lstat(filePath).then((stat) => {
  3. console.info("get link status succeed, the size of file is" + stat.size);
  4. }).catch((err) => {
  5. console.error("get link status failed with error message: " + err.message + ", error code: " + err.code);
  6. });

fs.lstat

lstat(path: string, callback: AsyncCallback<Stat>): void

獲取鏈接文件信息,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

callback

AsyncCallback<Stat>

回調(diào)函數(shù),返回文件的具體信息。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.lstat(filePath, (err, stat) => {
  3. if (err) {
  4. console.error("lstat failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.info("get link status succeed, the size of file is" + stat.size);
  7. }
  8. });

fs.lstatSync

lstatSync(path: string): Stat

以同步方法獲取鏈接文件信息。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

返回值:

類型

說明

Stat

表示文件的具體信息。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let stat = fs.lstatSync(filePath);

fs.rename

rename(oldPath: string, newPath: string): Promise<void>

重命名文件或文件夾,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

oldPath

string

文件的應用沙箱原路徑。

newPath

string

文件的應用沙箱新路徑。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcFile = pathDir + "/test.txt";
  2. let dstFile = pathDir + "/new.txt";
  3. fs.rename(srcFile, dstFile).then(() => {
  4. console.info("rename succeed");
  5. }).catch((err) => {
  6. console.error("rename failed with error message: " + err.message + ", error code: " + err.code);
  7. });

fs.rename

rename(oldPath: string, newPath: string, callback: AsyncCallback<void>): void

重命名文件或文件夾,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

oldPath

string

文件的應用沙箱原路徑。

newPath

string

文件的應用沙箱新路徑。

callback

AsyncCallback<void>

異步重命名文件之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcFile = pathDir + "/test.txt";
  2. let dstFile = pathDir + "/new.txt";
  3. fs.rename(srcFile, dstFile, (err) => {
  4. if (err) {
  5. console.error("rename failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("rename success");
  8. }
  9. });

fs.renameSync

renameSync(oldPath: string, newPath: string): void

以同步方法重命名文件或文件夾。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

oldPath

string

文件的應用沙箱原路徑。

newPath

string

文件的應用沙箱新路徑。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcFile = pathDir + "/test.txt";
  2. let dstFile = pathDir + "/new.txt";
  3. fs.renameSync(srcFile, dstFile);

fs.fsync

fsync(fd: number): Promise<void>

同步文件數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.fsync(file.fd).then(() => {
  4. console.info("sync data succeed");
  5. }).catch((err) => {
  6. console.error("sync data failed with error message: " + err.message + ", error code: " + err.code);
  7. });

fs.fsync

fsync(fd: number, callback: AsyncCallback<void>): void

同步文件數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

Callback

AsyncCallback<void>

異步將文件數(shù)據(jù)同步之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.fsync(file.fd, (err) => {
  4. if (err) {
  5. console.error("fsync failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("fsync success");
  8. fs.closeSync(file);
  9. }
  10. });

fs.fsyncSync

fsyncSync(fd: number): void

以同步方法同步文件數(shù)據(jù)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.fsyncSync(file.fd);
  4. fs.closeSync(file);

fs.fdatasync

fdatasync(fd: number): Promise<void>

實現(xiàn)文件內(nèi)容數(shù)據(jù)同步,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.fdatasync(file.fd).then((err) => {
  4. console.info("sync data succeed");
  5. fs.closeSync(file);
  6. }).catch((err) => {
  7. console.error("sync data failed with error message: " + err.message + ", error code: " + err.code);
  8. });

fs.fdatasync

fdatasync(fd: number, callback: AsyncCallback<void>): void

實現(xiàn)文件內(nèi)容數(shù)據(jù)同步,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

callback

AsyncCallback<void>

異步將文件內(nèi)容數(shù)據(jù)同步之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.fdatasync (file.fd, (err) => {
  4. if (err) {
  5. console.error("fdatasync failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("fdatasync success");
  8. fs.closeSync(file);
  9. }
  10. });

fs.fdatasyncSync

fdatasyncSync(fd: number): void

以同步方法實現(xiàn)文件內(nèi)容數(shù)據(jù)同步。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. let stat = fs.fdatasyncSync(file.fd);
  4. fs.closeSync(file);

fs.symlink

symlink(target: string, srcPath: string): Promise<void>

基于文件路徑創(chuàng)建符號鏈接,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

target

string

源文件的應用沙箱路徑。

srcPath

string

符號鏈接文件的應用沙箱路徑。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcFile = pathDir + "/test.txt";
  2. let dstFile = pathDir + "/test";
  3. fs.symlink(srcFile, dstFile).then(() => {
  4. console.info("symlink succeed");
  5. }).catch((err) => {
  6. console.error("symlink failed with error message: " + err.message + ", error code: " + err.code);
  7. });

fs.symlink

symlink(target: string, srcPath: string, callback: AsyncCallback<void>): void

基于文件路徑創(chuàng)建符號鏈接,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

target

string

源文件的應用沙箱路徑。

srcPath

string

符號鏈接文件的應用沙箱路徑。

callback

AsyncCallback<void>

異步創(chuàng)建符號鏈接信息之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcFile = pathDir + "/test.txt";
  2. let dstFile = pathDir + "/test";
  3. fs.symlink(srcFile, dstFile, (err) => {
  4. if (err) {
  5. console.error("symlink failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("symlink success");
  8. }
  9. });

fs.symlinkSync

symlinkSync(target: string, srcPath: string): void

以同步的方法基于文件路徑創(chuàng)建符號鏈接。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

target

string

源文件的應用沙箱路徑。

srcPath

string

符號鏈接文件的應用沙箱路徑。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcFile = pathDir + "/test.txt";
  2. let dstFile = pathDir + "/test";
  3. fs.symlinkSync(srcFile, dstFile);

fs.listFile

listFile(path: string, options?: {

recursion?: boolean;

listNum?: number;

filter?: Filter;

}): Promise<string[]>

列出文件夾下所有文件名,支持遞歸列出所有文件名(包含子目錄下),支持文件過濾,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件夾的應用沙箱路徑。

options

Object

文件過濾選項。默認不進行過濾。

options參數(shù)說明:

參數(shù)名

類型

必填

說明

recursion

boolean

是否遞歸子目錄下文件名,默認為false。

listNum

number

列出文件名數(shù)量。當設置0時,列出所有文件,默認為0。

filter

Filter

文件過濾選項。當前僅支持后綴名匹配、文件名模糊查詢、文件大小過濾、最近修改時間過濾。

返回值:

類型

說明

Promise<string[]>

Promise對象。返回文件名數(shù)組。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let options = {
  2. "recursion": false,
  3. "listNum": 0,
  4. "filter": {
  5. "suffix": [".png", ".jpg", ".jpeg"],
  6. "displayName": ["*abc", "efg*"],
  7. "fileSizeOver": 1024,
  8. "lastModifiedAfter": new Date().getTime(),
  9. }
  10. };
  11. fs.listFile(pathDir, options).then((filenames) => {
  12. console.info("listFile succeed");
  13. for (let i = 0; i < filenames.length; i++) {
  14. console.info("fileName: %s", filenames[i]);
  15. }
  16. }).catch((err) => {
  17. console.error("list file failed with error message: " + err.message + ", error code: " + err.code);
  18. });

fs.listFile

listFile(path: string, options?: {

recursion?: boolean;

listNum?: number;

filter?: Filter;

}, callback: AsyncCallback<string[]>): void

列出文件夾下所有文件名,支持遞歸列出所有文件名(包含子目錄下),支持文件過濾,使用Callback異步回調(diào)。

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件夾的應用沙箱路徑。

options

Object

文件過濾選項。默認不進行過濾。

callback

AsyncCallback<string[]>

異步列出文件名數(shù)組之后的回調(diào)。

options參數(shù)說明:

參數(shù)名

類型

必填

說明

recursion

boolean

是否遞歸子目錄下文件名,默認為false。

listNum

number

列出文件名數(shù)量。當設置0時,列出所有文件,默認為0。

filter

Filter

文件過濾選項。當前僅支持后綴名匹配、文件名模糊查詢、文件大小過濾、最近修改時間過濾。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let options = {
  2. "recursion": false,
  3. "listNum": 0,
  4. "filter": {
  5. "suffix": [".png", ".jpg", ".jpeg"],
  6. "displayName": ["*abc", "efg*"],
  7. "fileSizeOver": 1024,
  8. "lastModifiedAfter": new Date().getTime(),
  9. }
  10. };
  11. fs.listFile(pathDir, options, (err, filenames) => {
  12. if (err) {
  13. console.error("list file failed with error message: " + err.message + ", error code: " + err.code);
  14. } else {
  15. console.info("listFile succeed");
  16. for (let i = 0; i < filenames.length; i++) {
  17. console.info("filename: %s", filenames[i]);
  18. }
  19. }
  20. });

fs.listFileSync

listFileSync(path: string, options?: {

recursion?: boolean;

listNum?: number;

filter?: Filter;

}): string[];

以同步方式列出文件夾下所有文件名,支持遞歸列出所有文件名(包含子目錄下),支持文件過濾。

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件夾的應用沙箱路徑。

options

Object

文件過濾選項。默認不進行過濾。

options參數(shù)說明:

參數(shù)名

類型

必填

說明

recursion

boolean

是否遞歸子目錄下文件名,默認為false。

listNum

number

列出文件名數(shù)量。當設置0時,列出所有文件,默認為0。

filter

Filter

文件過濾選項。當前僅支持后綴名匹配、文件名模糊查詢、文件大小過濾、最近修改時間過濾。

返回值:

類型

說明

string[]

返回文件名數(shù)組。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let options = {
  2. "recursion": false,
  3. "listNum": 0,
  4. "filter": {
  5. "suffix": [".png", ".jpg", ".jpeg"],
  6. "displayName": ["*abc", "efg*"],
  7. "fileSizeOver": 1024,
  8. "lastModifiedAfter": new Date().getTime(),
  9. }
  10. };
  11. let filenames = fs.listFileSync(pathDir, options);
  12. console.info("listFile succeed");
  13. for (let i = 0; i < filenames.length; i++) {
  14. console.info("filename: %s", filenames[i]);
  15. }

fs.moveFile

moveFile(src: string, dest: string, mode?: number): Promise<void>;

移動文件,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

src

string

源文件的應用沙箱路徑。

dest

string

目的文件的應用沙箱路徑。

mode

number

移動模式。若mode為0,移動位置存在同名文件時,強制移動覆蓋。若mode為1,移動位置存在同名文件時,拋出異常。默認為0。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let srcPath = pathDir + "/source.txt";
  2. let destPath = pathDir + "/dest.txt";
  3. fs.moveFile(srcPath, destPath, 0).then(() => {
  4. console.info("move file succeed");
  5. }).catch((err) => {
  6. console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
  7. });

fs.moveFile

moveFile(src: string, dest: string, mode?: number, callback: AsyncCallback<void>): void;

移動文件,使用Callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

src

string

源文件的應用沙箱路徑。

dest

string

目的文件的應用沙箱路徑。

mode

number

移動模式。若mode為0,移動位置存在同名文件時,強制移動覆蓋。若mode為1,移動位置存在同名文件時,拋出異常。默認為0。

callback

AsyncCallback<void>

異步移動文件之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let srcPath = pathDir + "/source.txt";
  2. let destPath = pathDir + "/dest.txt";
  3. fs.moveFile(srcPath, destPath, 0, (err) => {
  4. if (err) {
  5. console.error("move file failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("move file succeed");
  8. }
  9. });

fs.moveFileSync

moveFile(src: string, dest: string, mode?: number): void;

以同步方式移動文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

src

string

源文件的應用沙箱路徑。

dest

string

目的文件的應用沙箱路徑。

mode

number

移動模式。若mode為0,移動位置存在同名文件時,強制移動覆蓋。若mode為1,移動位置存在同名文件時,拋出異常。默認為0。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let srcPath = pathDir + "/source.txt";
  2. let destPath = pathDir + "/dest.txt";
  3. fs.moveFileSync(srcPath, destPath, 0);
  4. console.info("move file succeed");

fs.mkdtemp

mkdtemp(prefix: string): Promise<string>

創(chuàng)建臨時目錄,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

prefix

string

用隨機產(chǎn)生的字符串替換以“XXXXXX”結(jié)尾目錄路徑。

返回值:

類型

說明

Promise<string>

Promise對象。返回生成的唯一目錄路徑。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. fs.mkdtemp(pathDir + "/XXXXXX").then((pathDir) => {
  2. console.info("mkdtemp succeed:" + pathDir);
  3. }).catch((err) => {
  4. console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code);
  5. });

fs.mkdtemp

mkdtemp(prefix: string, callback: AsyncCallback<string>): void

創(chuàng)建臨時目錄,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

prefix

string

用隨機產(chǎn)生的字符串替換以“XXXXXX”結(jié)尾目錄路徑。

callback

AsyncCallback<string>

異步創(chuàng)建臨時目錄之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. fs.mkdtemp(pathDir + "/XXXXXX", (err, res) => {
  2. if (err) {
  3. console.error("mkdtemp failed with error message: " + err.message + ", error code: " + err.code);
  4. } else {
  5. console.info("mkdtemp success");
  6. }
  7. });

fs.mkdtempSync

mkdtempSync(prefix: string): string

以同步的方法創(chuàng)建臨時目錄。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

prefix

string

用隨機產(chǎn)生的字符串替換以“XXXXXX”結(jié)尾目錄路徑。

返回值:

類型

說明

string

產(chǎn)生的唯一目錄路徑。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let res = fs.mkdtempSync(pathDir + "/XXXXXX");

fs.createStream

createStream(path: string, mode: string): Promise<Stream>

基于文件路徑打開文件流,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

mode

string

- r:打開只讀文件,該文件必須存在。

- r+:打開可讀寫的文件,該文件必須存在。

- w:打開只寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- w+:打開可讀寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- a:以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾,即文件原先的內(nèi)容會被保留。

- a+:以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾后,即文件原先的內(nèi)容會被保留。

返回值:

類型

說明

Promise<Stream>

Promise對象。返回文件流的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.createStream(filePath, "r+").then((stream) => {
  3. console.info("createStream succeed");
  4. }).catch((err) => {
  5. console.error("createStream failed with error message: " + err.message + ", error code: " + err.code);
  6. });

fs.createStream

createStream(path: string, mode: string, callback: AsyncCallback<Stream>): void

基于文件路徑打開文件流,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

mode

string

- r:打開只讀文件,該文件必須存在。

- r+:打開可讀寫的文件,該文件必須存在。

- w:打開只寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- w+:打開可讀寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- a:以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾,即文件原先的內(nèi)容會被保留。

- a+:以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾后,即文件原先的內(nèi)容會被保留。

callback

AsyncCallback<Stream>

異步打開文件流之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. fs.createStream(filePath, "r+", (err, stream) => {
  3. if (err) {
  4. console.error("create stream failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.info("create stream success");
  7. }
  8. });

fs.createStreamSync

createStreamSync(path: string, mode: string): Stream

以同步方法基于文件路徑打開文件流。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

path

string

文件的應用沙箱路徑。

mode

string

- r:打開只讀文件,該文件必須存在。

- r+:打開可讀寫的文件,該文件必須存在。

- w:打開只寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- w+:打開可讀寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- a:以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾,即文件原先的內(nèi)容會被保留。

- a+:以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾后,即文件原先的內(nèi)容會被保留。

返回值:

類型

說明

Stream

返回文件流的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss = fs.createStreamSync(filePath, "r+");

fs.fdopenStream

fdopenStream(fd: number, mode: string): Promise<Stream>

基于文件描述符打開文件流,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

mode

string

- r:打開只讀文件,該文件必須存在。

- r+:打開可讀寫的文件,該文件必須存在。

- w:打開只寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- w+:打開可讀寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- a:以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾,即文件原先的內(nèi)容會被保留。

- a+:以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾后,即文件原先的內(nèi)容會被保留。

返回值:

類型

說明

Promise<Stream>

Promise對象。返回文件流的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath);
  3. fs.fdopenStream(file.fd, "r+").then((stream) => {
  4. console.info("openStream succeed");
  5. fs.closeSync(file);
  6. }).catch((err) => {
  7. console.error("openStream failed with error message: " + err.message + ", error code: " + err.code);
  8. });

fs.fdopenStream

fdopenStream(fd: number, mode: string, callback: AsyncCallback<Stream>): void

基于文件描述符打開文件流,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

mode

string

- r:打開只讀文件,該文件必須存在。

- r+:打開可讀寫的文件,該文件必須存在。

- w:打開只寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- w+:打開可讀寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- a:以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾,即文件原先的內(nèi)容會被保留。

- a+:以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾后,即文件原先的內(nèi)容會被保留。

callback

AsyncCallback<Stream>

異步打開文件流之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY);
  3. fs.fdopenStream(file.fd, "r+", (err, stream) => {
  4. if (err) {
  5. console.error("fdopen stream failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("fdopen stream success");
  8. fs.closeSync(file);
  9. }
  10. });

fs.fdopenStreamSync

fdopenStreamSync(fd: number, mode: string): Stream

以同步方法基于文件描述符打開文件流。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

fd

number

已打開的文件描述符。

mode

string

- r:打開只讀文件,該文件必須存在。

- r+:打開可讀寫的文件,該文件必須存在。

- w:打開只寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- w+:打開可讀寫文件,若文件存在則文件長度清0,即該文件內(nèi)容會消失。若文件不存在則建立該文件。

- a:以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾,即文件原先的內(nèi)容會被保留。

- a+:以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數(shù)據(jù)會被加到文件尾后,即文件原先的內(nèi)容會被保留。

返回值:

類型

說明

Stream

返回文件流的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let file = fs.openSync(filePath, fs.OpenMode.READ_ONLY | fs.OpenMode.CREATE);
  3. let ss = fs.fdopenStreamSync(file.fd, "r+");
  4. fs.closeSync(file);

Stat

文件具體信息,在調(diào)用Stat的方法前,需要先通過stat()方法(同步或異步)來構(gòu)建一個Stat實例。

系統(tǒng)能力:以下各項對應的系統(tǒng)能力均為SystemCapability.FileManagement.File.FileIO。

屬性

名稱

類型

可讀

可寫

說明

ino

number

標識該文件。通常同設備上的不同文件的INO不同。

mode

number

表示文件權限,各特征位的含義如下:

- 0o400:用戶讀,對于普通文件,所有者可讀取文件;對于目錄,所有者可讀取目錄項。

- 0o200:用戶寫,對于普通文件,所有者可寫入文件;對于目錄,所有者可創(chuàng)建/刪除目錄項。

- 0o100:用戶執(zhí)行,對于普通文件,所有者可執(zhí)行文件;對于目錄,所有者可在目錄中搜索給定路徑名。

- 0o040:用戶組讀,對于普通文件,所有用戶組可讀取文件;對于目錄,所有用戶組可讀取目錄項。

- 0o020:用戶組寫,對于普通文件,所有用戶組可寫入文件;對于目錄,所有用戶組可創(chuàng)建/刪除目錄項。

- 0o010:用戶組執(zhí)行,對于普通文件,所有用戶組可執(zhí)行文件;對于目錄,所有用戶組是否可在目錄中搜索給定路徑名。

- 0o004:其他讀,對于普通文件,其余用戶可讀取文件;對于目錄,其他用戶組可讀取目錄項。

- 0o002:其他寫,對于普通文件,其余用戶可寫入文件;對于目錄,其他用戶組可創(chuàng)建/刪除目錄項。

- 0o001:其他執(zhí)行,對于普通文件,其余用戶可執(zhí)行文件;對于目錄,其他用戶組可在目錄中搜索給定路徑名。

uid

number

文件所有者的ID。

gid

number

文件所有組的ID。

size

number

文件的大小,以字節(jié)為單位。僅對普通文件有效。

atime

number

上次訪問該文件的時間,表示距1970年1月1日0時0分0秒的秒數(shù)。

mtime

number

上次修改該文件的時間,表示距1970年1月1日0時0分0秒的秒數(shù)。

ctime

number

最近改變文件狀態(tài)的時間,表示距1970年1月1日0時0分0秒的秒數(shù)。

isBlockDevice

isBlockDevice(): boolean

用于判斷文件是否是塊特殊文件。一個塊特殊文件只能以塊為粒度進行訪問,且訪問的時候帶緩存。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

boolean

表示文件是否是塊特殊設備。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let isBLockDevice = fs.statSync(filePath).isBlockDevice();

isCharacterDevice

isCharacterDevice(): boolean

用于判斷文件是否是字符特殊文件。一個字符特殊設備可進行隨機訪問,且訪問的時候不帶緩存。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

boolean

表示文件是否是字符特殊設備。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let isCharacterDevice = fs.statSync(filePath).isCharacterDevice();

isDirectory

isDirectory(): boolean

用于判斷文件是否是目錄。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

boolean

表示文件是否是目錄。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let dirPath = pathDir + "/test";
  2. let isDirectory = fs.statSync(dirPath).isDirectory();

isFIFO

isFIFO(): boolean

用于判斷文件是否是命名管道(有時也稱為FIFO)。命名管道通常用于進程間通信。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

boolean

表示文件是否是 FIFO。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let isFIFO = fs.statSync(filePath).isFIFO();

isFile

isFile(): boolean

用于判斷文件是否是普通文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

boolean

表示文件是否是普通文件。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let isFile = fs.statSync(filePath).isFile();

isSocket

isSocket(): boolean

用于判斷文件是否是套接字。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

boolean

表示文件是否是套接字。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let isSocket = fs.statSync(filePath).isSocket();

isSymbolicLink

isSymbolicLink(): boolean

用于判斷文件是否是符號鏈接。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

boolean

表示文件是否是符號鏈接。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test";
  2. let isSymbolicLink = fs.statSync(filePath).isSymbolicLink();

Stream

文件流,在調(diào)用Stream的方法前,需要先通過createStream()方法(同步或異步)來構(gòu)建一個Stream實例。

close

close(): Promise<void>

關閉文件流,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

Promise<void>

Promise對象。返回表示異步關閉文件流的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.close().then(() => {
  4. console.info("close fileStream succeed");
  5. }).catch((err) => {
  6. console.error("close fileStream failed with error message: " + err.message + ", error code: " + err.code);
  7. });

close

close(callback: AsyncCallback<void>): void

異步關閉文件流,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

異步關閉文件流之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.close((err) => {
  4. if (err) {
  5. console.error("close stream failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("close stream success");
  8. }
  9. });

closeSync

closeSync(): void

同步關閉文件流。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.closeSync();

flush

flush(): Promise<void>

刷新文件流,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

返回值:

類型

說明

Promise<void>

Promise對象。返回表示異步刷新文件流的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.flush().then(() => {
  4. console.info("flush succeed");
  5. }).catch((err) => {
  6. console.error("flush failed with error message: " + err.message + ", error code: " + err.code);
  7. });

flush

flush(callback: AsyncCallback<void>): void

異步刷新文件流,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

callback

AsyncCallback<void>

異步刷新文件流后的回調(diào)函數(shù)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.flush((err) => {
  4. if (err) {
  5. console.error("flush stream failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. console.info("flush success");
  8. }
  9. });

flushSync

flushSync(): void

同步刷新文件流。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.flushSync();

write

write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): Promise<number>

將數(shù)據(jù)寫入流文件,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

buffer

ArrayBuffer|string

待寫入文件的數(shù)據(jù),可來自緩沖區(qū)或字符串。

options

Object

支持如下選項:

- length,number類型,表示期望寫入數(shù)據(jù)的長度。默認緩沖區(qū)長度。

- offset,number類型,表示期望寫入文件的位置。可選,默認從當前位置開始寫。

- encoding,string類型,當數(shù)據(jù)是string類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8'。僅支持 'utf-8'。

返回值:

類型

說明

Promise<number>

Promise對象。返回實際寫入的長度。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.write("hello, world",{ offset: 5, length: 5, encoding: 'utf-8' }).then((number) => {
  4. console.info("write succeed and size is:" + number);
  5. }).catch((err) => {
  6. console.error("write failed with error message: " + err.message + ", error code: " + err.code);
  7. });

write

write(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }, callback: AsyncCallback<number>): void

將數(shù)據(jù)寫入流文件,使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

buffer

ArrayBuffer|string

待寫入文件的數(shù)據(jù),可來自緩沖區(qū)或字符串。

options

Object

支持如下選項:

- length,number類型,表示期望寫入數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

- offset,number類型,表示期望寫入文件的位置??蛇x,默認從當前位置開始寫。

- encoding,string類型,當數(shù)據(jù)是string類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8'。僅支持 'utf-8'。

callback

AsyncCallback<number>

異步寫入完成后執(zhí)行的回調(diào)函數(shù)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath, "r+");
  3. ss.write("hello, world", { offset: 5, length: 5, encoding :'utf-8'}, (err, bytesWritten) => {
  4. if (err) {
  5. console.error("write stream failed with error message: " + err.message + ", error code: " + err.code);
  6. } else {
  7. if (bytesWritten) {
  8. console.info("write succeed and size is:" + bytesWritten);
  9. }
  10. }
  11. });

writeSync

writeSync(buffer: ArrayBuffer|string, options?: { offset?: number; length?: number; encoding?: string; }): number

以同步方法將數(shù)據(jù)寫入流文件。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

buffer

ArrayBuffer|string

待寫入文件的數(shù)據(jù),可來自緩沖區(qū)或字符串。

options

Object

支持如下選項:

- length,number類型,表示期望寫入數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

- offset,number類型,表示期望寫入文件的位置??蛇x,默認從當前位置開始寫。

- encoding,string類型,當數(shù)據(jù)是string類型時有效,表示數(shù)據(jù)的編碼方式,默認 'utf-8'。僅支持 'utf-8'。

返回值:

類型

說明

number

實際寫入的長度。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss= fs.createStreamSync(filePath,"r+");
  3. let num = ss.writeSync("hello, world", {offset: 5, length: 5, encoding :'utf-8'});

read

read(buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): Promise<number>

從流文件讀取數(shù)據(jù),使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

buffer

ArrayBuffer

用于讀取文件的緩沖區(qū)。

options

Object

支持如下選項:

- length,number類型,表示期望讀取數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀。

返回值:

類型

說明

Promise<number>

Promise對象。返回讀取的結(jié)果。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss = fs.createStreamSync(filePath, "r+");
  3. let buf = new ArrayBuffer(4096);
  4. ss.read(buf, {offset: 5, length: 5}).then((readLen) => {
  5. console.info("read data succeed");
  6. console.log(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen))));
  7. }).catch((err) => {
  8. console.error("read data failed with error message: " + err.message + ", error code: " + err.code);
  9. });

read

read(buffer: ArrayBuffer, options?: { position?: number; offset?: number; length?: number; }, callback: AsyncCallback<number>): void

從流文件讀取數(shù)據(jù),使用callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

buffer

ArrayBuffer

用于讀取文件的緩沖區(qū)。

options

Object

支持如下選項:

- length,number類型,表示期望讀取數(shù)據(jù)的長度??蛇x,默認緩沖區(qū)長度。

- offset,number類型,表示期望讀取文件的位置??蛇x,默認從當前位置開始讀.

callback

AsyncCallback<number>

異步從流文件讀取數(shù)據(jù)之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss = fs.createStreamSync(filePath, "r+");
  3. let buf = new ArrayBuffer(4096)
  4. ss.read(buf, {offset: 5, length: 5}, (err, readLen) => {
  5. if (err) {
  6. console.error("read stream failed with error message: " + err.message + ", error code: " + err.code);
  7. } else {
  8. console.info("read data succeed");
  9. console.log(String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen))));
  10. }
  11. });

readSync

readSync(buffer: ArrayBuffer, options?: { offset?: number; length?: number; }): number

以同步方法從流文件讀取數(shù)據(jù)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

buffer

ArrayBuffer

用于讀取文件的緩沖區(qū)。

options

Object

支持如下選項:

- length,number類型,表示期望讀取數(shù)據(jù)的長度。可選,默認緩沖區(qū)長度。

- offset,number類型,表示期望讀取文件的位置。可選,默認從當前位置開始讀。

返回值:

類型

說明

number

實際讀取的長度。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let filePath = pathDir + "/test.txt";
  2. let ss = fs.createStreamSync(filePath, "r+");
  3. let num = ss.readSync(new ArrayBuffer(4096), {offset: 5, length: 5});

File

由open接口打開的File對象。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

屬性

名稱

類型

可讀

可寫

說明

fd

number

打開的文件描述符。

lock

lock(exclusive?: boolean): Promise<void>;

文件阻塞式施加共享鎖或獨占鎖,使用Promise異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

exclusive

boolean

是否施加獨占鎖,默認false。

返回值:

類型

說明

Promise<void>

Promise對象。無返回值。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  2. file.lock(true).then(() => {
  3. console.log("lock file successful");
  4. }).catch((err) => {
  5. console.info("lock file failed with error message: " + err.message + ", error code: " + err.code);
  6. });

lock

lock(exclusive?: boolean, callback: AsyncCallback<void>): void;

文件阻塞式施加共享鎖或獨占鎖,使Callback異步回調(diào)。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

exclusive

boolean

是否施加獨占鎖,默認false。

callback

AsyncCallback<void>

異步文件上鎖之后的回調(diào)。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  2. file.lock(true, (err) => {
  3. if (err) {
  4. console.error("lock file failed with error message: " + err.message + ", error code: " + err.code);
  5. } else {
  6. console.log("lock file successful");
  7. }
  8. });

tryLock

tryLock(exclusive?: boolean): void;

文件非阻塞式施加共享鎖或獨占鎖。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

參數(shù):

參數(shù)名

類型

必填

說明

exclusive

boolean

是否施加獨占鎖,默認false。

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼。

示例:

  1. let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  2. file.tryLock(true);
  3. console.log("lock file successful");

unlock

unlock(): void;

以同步方式給文件解鎖。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

錯誤碼:

接口拋出錯誤碼的詳細介紹請參見基礎文件IO錯誤碼

示例:

  1. let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
  2. file.tryLock(true);
  3. file.unlock();
  4. console.log("unlock file successful");

OpenMode

open接口flags參數(shù)常量。文件打開標簽。

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

名稱

類型

說明

READ_ONLY

number

0o0

只讀打開。

WRITE_ONLY

number

0o1

只寫打開。

READ_WRITE

number

0o2

讀寫打開。

CREATE

number

0o100

若文件不存在,則創(chuàng)建文件。

TRUNC

number

0o1000

如果文件存在且以只寫或讀寫的方式打開文件,則將其長度裁剪為零。

APPEND

number

0o2000

以追加方式打開,后續(xù)寫將追加到文件末尾。

NONBLOCK

number

0o4000

如果path指向FIFO、塊特殊文件或字符特殊文件,則本次打開及后續(xù) IO 進行非阻塞操作。

DIR

number

0o200000

如果path不指向目錄,則出錯。

NOFOLLOW

number

0o400000

如果path指向符號鏈接,則出錯。

SYNC

number

0o4010000

以同步IO的方式打開文件。

Filter

系統(tǒng)能力:SystemCapability.FileManagement.File.FileIO

文件過濾配置項類型,支持listFile接口使用。

名稱

類型

說明

suffix

Array<string>

文件后綴名完全匹配,各個關鍵詞OR關系。

displayName

Array<string>

文件名模糊匹配,各個關鍵詞OR關系。

mimeType

Array<string>

mime類型完全匹配,各個關鍵詞OR關系。

fileSizeOver

number

文件大小匹配,大于等于指定大小的文件。

lastModifiedAfter

number

文件最近修改時間匹配,在指定時間點及之后的文件。

excludeMedia

boolean

是否排除Media中已有的文件。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號