目录

FileSystemManager

文件管理器,可通过 has.getFileSystemManager 获取。

起始版本: 1.0.4

FileSystemManager.open

FileSystemManager.open(Object object): void

打开文件,返回文件描述符。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
filePath string - 是 文件路径 (本地路径),仅支持internal://协议类型。
flag string r 否 文件系统标志,合法值见FlagMode。
success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success回调函数:

参数为Object对象,包括以下字段。

参数 类型 描述
fd string 文件描述符。

错误码信息:

错误码 错误信息
102 unknown flag.
105 open:fail error: invalid path.
106 open:fail error: file already exists.
90002501 open:fail error.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.open({
  filePath: 'internal://cache/test.txt',
  success: (res) => {
    console.info('open success', res);
  },
  fail: (err) => {
    console.error('open fail', err);
  },
  complete: (res) => {
    console.info('open complete', res);
  }
});

FileSystemManager.openSync

FileSystemManager.openSync(Object object): string

同步打开文件,返回文件描述符。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
filePath string - 是 文件路径 (本地路径),仅支持internal://协议类型。
flag string r 否 文件系统标志,合法值见FlagMode。

返回值:

参数 类型 描述
fd string 文件描述符。

错误码信息:

错误码 错误信息
105 openSync:fail error: invalid path.
106 openSync:fail error: file already exists.
90002601 openSync:fail error.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});

FileSystemManager.close

FileSystemManager.close(Object object): void

关闭文件。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
fd string 是 需要被关闭的文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
102 close fail: The fd is invalid or the fd already closed.
90002701 close:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.close({
  fd: fd,
  success: () => {
    console.info('close success');
  },
  fail: (err) => {
    console.error('close fail', err);
  },
  complete: (res) => {
    console.info('close complete', res);
  }
});

FileSystemManager.closeSync

FileSystemManager.closeSync(Object object): void

同步关闭文件。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
fd string 是 需要被关闭的文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。

错误码信息:

错误码 错误信息
102 closeSync fail: The fd is invalid or the fd already closed.
90002801 closeSync:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.closeSync({
  fd: fd
});

FileSystemManager.read

FileSystemManager.read(Object object): void

读文件。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
fd string - 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
arrayBuffer ArrayBuffer - 是 数据写入的缓冲区,必须是ArrayBuffer实例。
offset number 0 否 缓冲区中的写入偏移量,默认值:0。
length number 0 否 要从文件中读取的字节数,默认值:0。
position number - 否 文件读取的起始位置,如不传或传null,则会从当前文件指针的位置读取。如果position是大于等于0整数,则文件指针位置会保持不变并从position读取文件。
success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success回调函数:

参数为Object对象,包括以下字段。

参数 类型 描述
bytesRead number 实际读取的字节数。
arrayBuffer ArrayBuffer 被写入的缓存区的对象,即接口入参的arrayBuffer。

错误码信息:

错误码 错误信息
102

1. read fail: The fd is invalid or the fd already closed.

2. read fail: The offset must be >=0.

3. read fail: The length must be >=0.

4. read fail: The position must be >=0.

90001801 read failed with error message.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.read({
  fd: fd,
  arrayBuffer: new ArrayBuffer(10),
  length: 10,
  success: (res) => {
    console.info('read success', res.bytesRead);
  },
  fail: (err) => {
    console.error('read fail', err);
  },
  complete: (res) => {
    console.info('read complete', res);
  }
});

FileSystemManager.readSync

FileSystemManager.readSync(Object object): ReadResult

同步读文件。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
fd string - 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
arrayBuffer ArrayBuffer - 是 数据写入的缓冲区,必须是arrayBuffer类型实例。
offset number 0 否

缓冲区中的写入偏移量(单位:字节)。

推荐取值范围:[0, arrayBuffer的字节长度]。如取值小于0,则取0,如取值大于arrayBuffer的长度,则取arrayBuffer的长度。

length number 0 否 要从文件中读取的字节数(单位:字节)。
position number - 否 文件读取的起始位置,如不传或传null,则会从当前文件指针的位置读取。如果position是大于等于0整数,则文件指针位置会保持不变并从position读取文件。

返回值:

ReadResult,文件读取结果,为Object对象,包括以下字段。

参数 类型 描述
bytesRead number 实际读取的字节数。
arrayBuffer ArrayBuffer 被写入的缓存区的对象,即接口入参的arrayBuffer。

错误码信息:

错误码 错误信息
102

1. readSync fail: The fd is invalid or the fd already closed.

2. readSync fail: The offset must be >=0.

3. readSync fail: The length must be >=0.

4. readSync fail: The position must be >=0.

90001901 readSync:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
const readResult = fileSystemManager.readSync({
  fd: fd,
  arrayBuffer: new ArrayBuffer(10),
  length: 10
});
console.info('bytesRead', readResult.bytesRead);
console.info('arrayBuffer', readResult.arrayBuffer);

FileSystemManager.fstat

FileSystemManager.fstat(Object object): void

获取文件的状态信息。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
fd string 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success回调函数:

参数为Object对象,包括以下字段。

参数 类型 描述
stats Stats Stats对象,包含了文件的状态信息。

错误码信息:

错误码 错误信息
102 fstat fail: The fd is invalid or the fd already closed.
90003401 fstat error:

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt'
});
fileSystemManager.fstat({
  fd: fd,
  success: (res) => {
    console.info('fstat success', res);
    console.info('fstat success', res.stats.isFile());
  },
  fail: (err) => {
    console.error('fstat fail', err);
  },
  complete: (res) => {
    console.info('fstat complete', res);
  }
});

FileSystemManager.fstatSync

FileSystemManager.fstatSync(Object object): Stats

同步获取文件的状态信息。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
fd string 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。

返回值:

参数 类型 描述
stats Stats 文件的状态信息对象。

错误码信息:

错误码 错误信息
102 fstatSync fail: The fd is invalid or the fd already closed.
90003501 fstatSync error.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  const fd = fileSystemManager.openSync({
    filePath: 'internal://cache/test.txt'
  });
  const stats = fileSystemManager.fstatSync({
    fd: fd
  });
  console.info('fstatSync result', stats);
  console.info('fstatSync result', stats.isFile());
} catch (err) {
  console.error('fstatSync error', err);
}

FileSystemManager.ftruncate

FileSystemManager.ftruncate(Object object): void

对文件内容进行截断操作。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
fd string 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
length number 是

截断位置。单位:Byte。

如果length小于文件长度,则只有前面length个字节会保留在文件中,其余内容会被删除;如果length大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')。

success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
102

1. ftruncate:fail the length must be greater than 0.

2. ftruncate fail: The fd is invalid or the fd already closed.

108 ftruncate:fail the filePath is not writeable.
90003101 ftruncate:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt', // 文件需要写入权限
  flag: 'r+'
});
fileSystemManager.ftruncate({
  fd: fd,
  length: 0,
  success: () => {
    console.info('ftruncate success');
  },
  fail: (err) => {
    console.error('ftruncate fail', err);
  },
  complete: (res) => {
    console.info('ftruncate complete', res);
  }
});

FileSystemManager.ftruncateSync

FileSystemManager.ftruncateSync(Object object): void

同步对文件内容进行截断操作。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
fd string 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
length number 是

截断位置。单位:Byte。

如果length小于文件长度,则只有前面length个字节会保留在文件中,其余内容会被删除;如果length大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')。

错误码信息:

错误码 错误信息
102

1. ftruncateSync fail: The fd is invalid or the fd already closed.

2. ftruncateSync:fail the length must be greater than 0.

105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 ftruncateSync:fail the filePath is not writeable.
90003201 ftruncateSync:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt', // 文件需要写入权限
  flag: 'r+'
});
fileSystemManager.ftruncateSync({
  fd: fd,
  length: 0
});

FileSystemManager.truncate

FileSystemManager.truncate(Object object): void

对文件内容进行截断操作。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
filePath string - 是 要截断的文件路径 (本地路径),仅支持internal://协议类型。
length number 0 否

截断位置。单位:Byte。

如果length小于文件长度,则只有前面length个字节会保留在文件中,其余内容会被删除;如果length大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')。

推荐取值范围:[0,文件长度]。

success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
102 truncate:fail the length must be greater than 0.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 truncate:fail the filePath is not writeable.
90002901 truncate failed with error message.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.truncate({
  filePath: 'internal://cache/test.txt',
  length: 0,
  success: () => {
    console.info('truncate success');
  },
  fail: (err) => {
    console.error('truncate fail', err);
  },
  complete: (res) => {
    console.info('truncate complete', res);
  }
});

FileSystemManager.truncateSync

FileSystemManager.truncateSync(Object object): void

同步对文件内容进行截断操作。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
filePath string - 是 要截断的文件路径 (本地路径)。
length number 0 否 截断位置。如果length小于文件长度(单位:字节),则只有前面length个字节会保留在文件中,其余内容会被删除;如果length大于文件长度,则会对其进行扩展,并且扩展部分将填充空字节('\0')。

错误码信息:

错误码 错误信息
102 truncate:fail the length must be greater than 0.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 truncate:fail the filePath is not writeable.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.truncateSync({
  filePath: 'internal://cache/test.txt',
  length: 0
});

FileSystemManager.write

FileSystemManager.write(Object object): void

写入文件。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
fd string - 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
data string | arrayBuffer - 是 写入的内容,类型为string或arrayBuffer。
offset number 0 否 只在data类型是arrayBuffer时有效,决定arrayBuffer中要被写入的部位,即arrayBuffer中的索引。
length number 0 否 只在data类型是arrayBuffer时有效,指定要写入的字节数,默认为arrayBuffer从0开始偏移offset个字节后剩余的字节数。
encoding string utf-8 否

只在data类型是string时有效,指定写入文件的字符编码。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

position number - 否 指定文件开头的偏移量,即数据要被写入的位置。当position不传或者传入null时,数据会被写入当前指针所在位置。
success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success回调函数:

参数为Object对象,包括以下字段。

参数 类型 描述
bytesWritten number 实际被写入到文件中的字节数(注意,被写入的字节数不一定与被写入的字符串字符数相同)。

错误码信息:

错误码 错误信息
102

1. write:fail invalid fd.

2. write:fail.

90000701 write:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt',
  flag: 'r+'
});
fileSystemManager.write({
  fd: fd,
  data: 'some text',
  success: (res) => {
    console.info('write success', res.bytesWritten);
  },
  fail: (err) => {
    console.error('write fail', err);
  },
  complete: (res) => {
    console.info('write complete', res);
  }
});

FileSystemManager.writeSync

FileSystemManager.writeSync(Object object): WriteResult

同步写入文件。

起始版本: 1.0.16

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
fd string - 是 文件描述符。fd通过FileSystemManager.open或FileSystemManager.openSync接口获得。
data string | arrayBuffer - 是 写入的内容,类型为string或arrayBuffer。
offset number 0 否 只在data类型是arrayBuffer时有效,决定arrayBuffer中要被写入的部位,即arrayBuffer中的索引。
length number 0 否 只在data类型是arrayBuffer时有效,指定要写入的字节数,默认为arrayBuffer从0开始偏移offset个字节后剩余的字节数。
encoding string utf-8 否

只在data类型是string时有效,指定写入文件的字符编码。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

position number - 否 指定文件开头的偏移量,即数据要被写入的位置。当position不传或者传入null时,数据会被写入当前指针所在位置。

返回值:

返回文件写入结果WriteResult,类型为Object对象。

参数 类型 描述
bytesWritten number 实际被写入到文件中的字节数(注意,被写入的字节数不一定与被写入的字符串字符数相同)。

错误码信息:

错误码 错误信息
102 writeSync fail: The fd is invalid or the fd already closed.
90000801 writeSync:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const fd = fileSystemManager.openSync({
  filePath: 'internal://cache/test.txt',
  flag: 'r+'
});
const writeResult = fileSystemManager.writeSync({
  fd: fd,
  data: 'some text'
});
console.info('writeSync.bytesWritten ', writeResult.bytesWritten);

FileSystemManager.access

FileSystemManager.access(Object object)

异步判断一个文件/目录是否存在。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
path string 是 要判断是否存在的文件/目录路径,支持internal://协议类型,支持资源文件绝对路径。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

fail返回值:

若不存在则返回:

{errMsg: 'access:fail no such file or directory.'}

错误码信息:

错误码 错误信息
101

1. access:error.

2. access:fail, message.

3. context is undefined.

104 access:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.access({
  path: 'internal://cache/demo.png',
  success: () => {
    console.info('access success');
  },
  fail: (err) => {
    console.error('access fail', err);
  },
  complete: (res) => {
    console.info('access complete', res);
  }
});

FileSystemManager.accessSync

FileSystemManager.accessSync(string path): void

同步判断一个文件/目录是否存在。存在则正常执行,不存在抛出异常。

起始版本: 1.0.4

参数:

参数 类型 必填 描述
path string 是 要判断是否存在的文件/目录路径,支持internal://协议类型,支持资源文件绝对路径。

错误码信息:

错误码 错误信息
101 accessSync:error.
104 ccessSync:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.accessSync('internal://cache/demo.png');
} catch (err) {
  console.error('accessSync error', err);
}

FileSystemManager.appendFile

FileSystemManager.appendFile(Object object)

在文件结尾追加内容。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
filePath string - 是 需要追加的文件路径,仅支持internal://协议类型。
data string | ArrayBuffer - 是 需要追加的文本或二进制数据。(ArrayBuffer类型支持情况依据框架是否支持决定)。
encoding string utf-8 否

只在data类型是string时有效,指定写入文件的字符编码。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
102 appendFile:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 appendFile:fail the filePath is not writeable.
90000101 appendFile:fail can not write data to the file.
90000102 appendFile:fail can not open file. It may not be a valid file.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.appendFile({
  filePath: 'internal://cache/demo.png',
  data: '附件内容',
  success: () => {
    console.info('appendFile success');
  },
  fail: (err) => {
    console.error('appendFile fail', err);
  },
  complete: (res) => {
    console.info('appendFile complete', res);
  }
});

FileSystemManager.appendFileSync

FileSystemManager.appendFileSync(string filePath, string|ArrayBuffer data, string encoding): void

在文件结尾追加内容。成功则正常执行,失败抛出异常。(ArrayBuffer类型支持情况依据框架是否支持决定)

起始版本: 1.0.4

参数:

参数 类型 默认值 必填 描述
filePath string - 是 需要追加的文件路径,仅支持internal://协议类型。
data string | ArrayBuffer - 是 需要追加的文本或二进制数据。
encoding string utf-8 否

只在data类型是string时有效,指定写入文件的字符编码。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

错误码信息:

错误码 错误信息
102 appendFile:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 appendFile:fail the filePath is not writeable.
90000202 appendFile:fail can not open file. It may not be a valid file.
90000201 appendFile:fail can not write data to the file.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.appendFileSync('internal://cache/demo.png', '附件内容', 'utf-8');
} catch (err) {
  console.error('appendFileSync error', err);
}

FileSystemManager.copyFile

FileSystemManager.copyFile(Object object)

异步复制文件接口。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
srcPath string 是

源文件路径,支持以下两种类型的路径:

- 支持file://协议路径,通常用于从文件选择器中选择的文件。

- 支持internal://协议类型。

destPath string 是 目标文件路径,仅支持internal://files/和internal://cache/类型。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

fail返回值:

{ errMsg: 'copyFile:fail  原因' }

错误码信息:

错误码 错误信息
102 permission denied, invalid picker path.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 copyFile:fail the filePath is not writeable.
90001101 copyFile:fail to copy the file.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.copyFile({
  srcPath: 'internal://cache/demo.png',
  destPath: 'internal://cache/demo_copy.png',
  success: () => {
    console.info('copyFile success');
  },
  fail: (err) => {
    console.error('copyFile fail', err);
  },
  complete: (res) => {
    console.info('copyFile complete', res);
  }
});

FileSystemManager.copyFileSync

FileSystemManager.copyFileSync(string srcPath, string destPath): void

同步复制文件接口。成功则正常执行,失败抛出异常。

起始版本: 1.0.4

参数:

参数 类型 必填 描述
srcPath string 是

源文件路径,支持以下两种类型的路径:

- 支持file://协议路径,通常用于从文件选择器中选择的文件。

- 支持internal://协议类型。

destPath string 是 目标文件路径,仅支持internal://files/和internal://cache/类型。

错误码信息:

错误码 错误信息
102 permission denied, invalid picker path.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 copyFileSync:fail the filePath is not writeable.
90001201 copyFileSync:fail to copy the file.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.copyFileSync('internal://cache/demo.png', 'internal://cache/demo_copy.png');
} catch (err) {
  console.error('copyFileSync error', err);
}

FileSystemManager.getFileInfo

FileSystemManager.getFileInfo(Object object)

获取该元服务下的本地临时文件或本地缓存文件信息。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
filePath string - 是 要读取的文件路径,仅支持internal://协议类型。
digestAlgorithm string md5 否

计算文件摘要的算法,可选项如下:

- md5

- sha1

- sha256。

success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值:

返回值为Object对象,包括以下字段。

属性 类型 描述
size number 文件大小,单位:字节。
digest string 按照传入的digestAlgorithm计算得出的文件摘要。

错误码信息:

错误码 错误信息
101 getFileInfo:fail.
102 getFileInfo:fail invalid digestAlgorithm.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.getFileInfo({
  filePath: 'internal://cache/demo.png',
  digestAlgorithm: 'sha256',
  success: (res) => {
    console.info('getFileInfo success', res);
  },
  fail: (err) => {
    console.error('getFileInfo fail', err);
  },
  complete: (res) => {
    console.info('getFileInfo complete', res);
  }
});

FileSystemManager.getSavedFileList

FileSystemManager.getSavedFileList(Object object)

获取该元服务下的internal://files/store/下的文件列表。(保存缓存文件时,需写入到指定路径internal://files/store/下)

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值:

返回值为Object对象,包括以下字段。

属性 类型 描述
fileList Array<object> 文件数组,每一项都是一个FileItem。

fileList说明:

属性 类型 描述
filePath string 本地路径。
size number 本地文件大小,单位:Byte。
createTime number 文件保存时的时间戳,从1970/07/07 08:00:00到当前时间的秒数。

错误码信息:

错误码 错误信息
101 getSavedFileList:fail.
90001301 getSavedFileList:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.getSavedFileList({
  success: (res) => {
    console.info('getSavedFileList success', res);
  },
  fail: (err) => {
    console.error('getSavedFileList fail', err);
  },
  complete: (res) => {
    console.info('getSavedFileList complete', res);
  }
});

FileSystemManager.mkdir

FileSystemManager.mkdir(Object object)

创建目录。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
dirPath string - 是 创建的目录路径,仅支持internal://files/和internal://cache/类型。
recursive boolean false 否 是否在递归创建该目录的上级目录后再创建该目录。
success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
101 fail, recursive is false and upper path does not exist.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

106 dirPath already exists.
90000301 mkdir failed with error message.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.mkdir({
  dirPath: 'internal://files/example',
  recursive: false,
  success: () => {
    console.info('mkdir success');
  },
  fail: (err) => {
    console.error('mkdir fail', err);
  },
  complete: (res) => {
    console.info('mkdir complete', res);
  }
});

FileSystemManager.mkdirSync

FileSystemManager.mkdirSync(string dirPath, boolean recursive): void

同步创建目录。

起始版本: 1.0.4

参数:

参数 类型 默认值 必填 描述
dirPath string - 是 创建的目录路径,仅支持internal://files/和internal://cache/类型。
recursive boolean false 否 是否在递归创建该目录的上级目录后再创建该目录。

错误码信息:

错误码 错误信息
101 fail, recursive is false and upper path does not exist.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

106 dirPath already exists.
90000401 mkdirSync.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.mkdirSync('internal://files/example', false);
} catch (err) {
  console.error('mkdirSync error', err);
}

FileSystemManager.readdir

FileSystemManager.readdir(Object object)

读取目录内文件列表。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
dirPath string 是 要读取的目录路径 (本地路径)。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值:

返回值为Object对象,包括以下字段。

属性 类型 描述
files Array<string> 指定目录下的文件名数组。

错误码信息:

错误码 错误信息
104 readdir:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. readdir:fail not a directory.

6. readdir:fail parameter error: Invalid filePath.

90000901

1. readdir:error.

2. readdir failed with error message.

更多错误信息参考:基础文件IO错误码

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.readdir({
  dirPath: 'internal://files/example',
  success: (res) => {
    console.info('readdir success', res);
  },
  fail: (err) => {
    console.error('readdir fail', err);
  },
  complete: (res) => {
    console.info('readdir complete', res);
  }
});

FileSystemManager.readdirSync

FileSystemManager.readdirSync(string dirPath): Array<string>

同步读取目录内文件列表(本地路径)。

起始版本: 1.0.4

参数:

参数 类型 必填 描述
dirPath string 是 要读取的目录路径(本地路径)。

返回值:

类型 描述
Array<string> 指定目录下的文件名数组。

错误码信息:

错误码 错误信息
104 readdirSync:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. readdirSync:fail not a directory.

6. readdirSync:fail parameter error: Invalid filePath.

90001001

1. readdirSync: error.

2. readdirSync failed with error message:

更多错误信息参考:基础文件IO错误码

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  const res = fileSystemManager.readdirSync('internal://files/example');
  console.info('readdirSync success', res);
} catch (err) {
  console.error('readdirSync error', err);
}

FileSystemManager.readFile

FileSystemManager.readFile(Object object)

读取本地文件内容。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
filePath string 是 要读取的文件的路径(本地路径),仅支持internal://协议类型。
encoding string 否

指定读取文件的字符编码,如果不传encoding,则以ArrayBuffer格式读取文件的二进制内容。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

position number 否 从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间[position, position+length)。有效范围:[0, fileLength - 1]。单位:Byte。
length number 否 指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:Byte。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值:

返回值为Object对象,包括以下字段。

属性 类型 描述
data string | ArrayBuffer 文件内容。

错误码信息:

错误码 错误信息
102

1. fail the encoding parameter is invalid.

2. fail no such file or directory.

3. fail value of length is out of range.

4. fail value of position is out of range.

104 fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail not a file.

90001801 readFile failed with error message.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.readFile({
  filePath: 'internal://files/hello.txt',
  encoding: 'utf-8',
  position: 0,
  success: (res) => {
    console.info('readFile success', res);
  },
  fail: (err) => {
    console.error('readFile fail', err);
  },
  complete: (res) => {
    console.info('readFile complete', res);
  }
});

FileSystemManager.readFileSync

FileSystemManager.readFileSync(string filePath, string encoding, number position, number length): string|ArrayBuffer

同步读取本地文件内容。

起始版本: 1.0.4

参数:

参数 类型 必填 描述
filePath string 是 要读取的文件的路径(本地路径),仅支持internal://协议类型。
encoding string 否

指定读取文件的字符编码,如果不传encoding,则以ArrayBuffer格式读取文件的二进制内容。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

position number 否 从文件指定位置开始读,如果不指定,则从文件头开始读。读取的范围应该是左闭右开区间 [position, position+length)。有效范围:[0, fileLength - 1]。单位:Byte。
length number 否 指定文件的长度,如果不指定,则读到文件末尾。有效范围:[1, fileLength]。单位:Byte。

返回值:

属性 类型 描述
data string | ArrayBuffer 文件内容。

错误码信息:

错误码 错误信息
102

1. fail the encoding parameter is invalid.

2. fail value of length is out of range.

3. fail value of position is out of range.

104 fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail parameter error: Invalid filePath.

6. fail not a file.

90001901 readFileSync failed with error message.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  const res = fileSystemManager.readFileSync('internal://files/hello.txt', 'utf-8', 0);
  console.info('readFileSync success', res);
} catch (err) {
  console.error('readFileSync error', err);
}

FileSystemManager.removeSavedFile

FileSystemManager.removeSavedFile(Object object)

删除该元服务下已保存的本地缓存文件

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
filePath string 是 需要删除的文件路径(仅在internal://files/store/路径下操作)。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail parameter error: Invalid filePath.

6. fail file is directory.

104 fail no such file or directory.
90001701 removeSavedFile: fail.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.removeSavedFile({
  filePath: 'internal://files/store/hello.txt',
  success: () => {
    console.info('removeSavedFile success');
  },
  fail: (err) => {
    console.error('removeSavedFile fail', err);
  },
  complete: (res) => {
    console.info('removeSavedFile complete', res);
  }
});

FileSystemManager.rename

FileSystemManager.rename(Object object)

重命名文件。可以把文件从oldPath移动到newPath。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
oldPath string 是 源文件路径,支持本地路径。
newPath string 是 新文件路径,支持本地路径,仅支持internal://files/和internal://cache/类型。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
101 rename:fail.
104 rename:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 rename:fail the newPath is not writeable.
90001501 rename:fail error message.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.rename({
  oldPath: 'internal://files/hello.txt',
  newPath: 'internal://files/hello_new.txt',
  success: () => {
    console.info('rename success');
  },
  fail: (err) => {
    console.error('rename fail', err);
  },
  complete: (res) => {
    console.info('rename complete', res);
  }
});

FileSystemManager.renameSync

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

同步重命名文件。可以把文件从oldPath移动到newPath。

起始版本: 1.0.4

参数:

参数 类型 必填 描述
oldPath string 是 源文件路径,支持本地路径。
newPath string 是 新文件路径,支持本地路径。

错误码信息:

错误码 错误信息
101 renameSync:fail.
104 renameSync:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 rename:fail the newPath is not writeable.
90001601 renameSync:fail error message.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.renameSync('internal://files/hello.txt', 'internal://files/hello_new.txt');
} catch(err) {
  console.error('renameSync error', err);
}

FileSystemManager.rmdir

FileSystemManager.rmdir(Object object)

删除目录。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
dirPath string - 是 要删除的目录路径,仅支持internal://files/和internal://cache/类型。
recursive boolean false 否 是否递归删除目录。
success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
104 fail no such directory.
105 fail parameter error: Invalid path.
107 fail directory not empty.
90000501 rmdir failed with error message.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.rmdir({
  dirPath: 'internal://files/example',
  recursive: false,
  success: () => {
    console.info('rmdir success');
  },
  fail: (err) => {
    console.error('rmdir fail', err);
  },
  complete: (res) => {
    console.info('rmdir complete', res);
  }
});

FileSystemManager.rmdirSync

FileSystemManager.rmdirSync(string dirPath, boolean recursive): void

同步删除目录。

起始版本: 1.0.4

参数:

参数 类型 默认值 必填 描述
dirPath string - 是 要删除的目录路径,仅支持internal://files/和internal://cache/类型。
recursive boolean false 否 是否递归删除目录。

错误码信息:

错误码 错误信息
104 fail no such directory.
105 fail parameter error: Invalid path.
107 fail directory not empty.
90000601 rmdirSync.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.rmdirSync('internal://files/example', false);
} catch (err) {
  console.error('rmdirSync error', err);
}

FileSystemManager.saveFile

FileSystemManager.saveFile(Object object)

保存临时文件到本地。此接口会移动临时文件,因此调用成功后(tempFilePath:internal://tmp/, filePath: internal://files/store/),tempFilePath将不可用。

仅当tempFilePath传入temp目录下的文件路径(internal://tmp/)时,才允许filePath传入store目录下的文件路径(internal://files/store/)。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
tempFilePath string 是 临时存储文件路径,仅支持internal://协议类型。
filePath string 否 要存储的文件路径,仅支持internal://files/和internal://cache/类型。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值:

返回值为Object对象,包括以下字段。

属性 类型 描述
savedFilePath string 存储后的文件路径(本地路径)。

错误码信息:

错误码 错误信息
104 saveFile:fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 saveFile:fail the filePath is not writeable.
90001401 saveFile:fail init store path fail.
90001402 saveFile: directory is not supported.
90001403 saveFile: Failed to copy.
90001404 saveFile:fail illegal Operation on a directory.
90001405 saveFile:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.saveFile({
  tempFilePath: 'internal://tmp/templete.png',
  success: (res) => {
    console.info('saveFile success', res);
  },
  fail: (err) => {
    console.error('saveFile fail', err);
  },
  complete: (res) => {
    console.info('saveFile complete', res);
  }
});

FileSystemManager.saveFileSync

FileSystemManager.saveFileSync(string tempFilePath, string filePath): string

同步保存临时文件到本地。

仅当tempFilePath传入temp目录下的文件路径(internal://tmp/)时,才允许filePath传入store目录下的文件路径(internal://files/store/)。

起始版本: 1.0.4

参数:

参数 类型 必填 描述
tempFilePath string 是 临时存储文件路径,仅支持internal://协议类型。
filePath string 否 要存储的文件路径,仅支持internal://files/和internal://cache/类型。

返回值:

属性 类型 描述
savedFilePath string 存储后的文件路径(本地路径)。

错误码信息:

错误码 错误信息
101 saveFileSync:fail.
104 saveFileSync:fail no such file.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108 saveFileSync:fail the filePath is not writeable.
90001401 saveFileSync:fail init store path fail.

示例:

const fileSystemManager = has.getFileSystemManager();
const savedFilePath = fileSystemManager.saveFileSync('internal://tmp/demo.png');

FileSystemManager.stat

FileSystemManager.stat(Object object)

获取文件Stats对象。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
path string - 是 文件/目录路径,仅支持internal://协议类型。
recursive boolean false 否 是否递归获取目录下的每个文件的Stats信息。
success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值:

返回值为Object对象,包括以下字段。

属性 类型 描述
stats Stats|Array<FileStats>

当recursive为false时,res.stats是一个Stats对象。

当recursive为true且path是一个目录的路径时,res.stats是一个Array,数组的每一项是一个对象,每个对象包含path和stats。

错误码信息:

错误码 错误信息
105

stat:fail parameter error: Invalid path.

更多错误信息参考:基础文件IO错误码

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.stat({
  path: 'internal://cache/demo.png',
  recursive: false,
  success: (res) => {
    console.info('stat success', res);
  },
  fail: (err) => {
    console.error('stat fail', err);
  },
  complete: (res) => {
    console.info('stat complete', res);
  }
});

FileSystemManager.statSync

FileSystemManager.statSync(string path, boolean recursive): Object

同步获取文件Stats对象。

起始版本: 1.0.4

参数:

参数 类型 默认值 必填 描述
path string - 是 文件/目录路径,仅支持internal://协议类型。
recursive boolean false 否 是否递归获取目录下的每个文件的Stats信息。

返回值:

返回值为Object对象,包括以下字段。

属性 类型 描述
stats Stats|Array<FileStats>

当recursive为false时,stats是一个Stats对象。

当recursive为true且path是一个目录的路径时stats是一个Array,数组的每一项是一个对象,每个对象包含path和stats。

错误码信息:

错误码 错误信息
105

statSync:fail parameter error: Invalid path.

更多错误信息参考:基础文件IO错误码

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  const res = fileSystemManager.statSync('internal://cache/demo.png');
  console.info('statSync result', res);
} catch (err) {
  console.error('statSync error', err);
}

FileSystemManager.unlinkSync

FileSystemManager.unlinkSync(string filePath): void

同步删除文件。

起始版本: 1.0.4

参数:

参数 类型 必填 描述
filePath string 是 要删除的文件路径,仅支持internal://协议类型。

错误码信息:

错误码 错误信息
104 fail no such file or directory.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

5. fail parameter error: Invalid filePath.

6. fail file is directory.

90002301 unlinkSync:fail.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.unlinkSync('internal://cache/demo.png');

FileSystemManager.unzip

FileSystemManager.unzip(Object object)

解压文件。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 必填 描述
zipFilePath string 是 源文件路径,仅支持internal://协议类型(只可以是zip压缩文件)。
targetPath string 是 目标目录路径,仅支持internal://files/和internal://cache/类型。
success function 否 接口调用成功的回调函数。
fail function 否 接口调用失败的回调函数。
complete function 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
104 unzip:fail zipFilePath no such file or directory.
105

1. unzip:fail parameter error: Invalid zipFilePath.

2. fail parameter error: path is empty.

3. fail parameter error: parameter.path should be String.

4. fail parameter error: Invalid path.

5. fail parameter error: path is too long.

6. unzip:fail parameter error: Invalid targetPath.

7. unzip:fail zipFilePath not a file.

8. unzip:fail targetPath not a directory.

更多错误信息参考:基础文件IO错误码

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.unzip({
  zipFilePath: 'internal://cache/example.zip',
  targetPath: 'internal://cache/example',
  success: () => {
    console.info('unzip success');
  },
  fail: (err) => {
    console.error('unzip fail', err);
  },
  complete: (res) => {
    console.info('unzip complete', res);
  }
});

FileSystemManager.writeFile

FileSystemManager.writeFile(Object object)

写文件。

起始版本: 1.0.4

参数:

参数为Object对象,包括以下字段。

参数 类型 默认值 必填 描述
filePath string - 是 要写入的文件路径,仅支持internal://files/和internal://cache/类型。
data string | ArrayBuffer - 是 要写入的文本或二进制数据。
encoding string utf-8 否

只在data类型是string时有效,指定写入文件的字符编码。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

success function - 否 接口调用成功的回调函数。
fail function - 否 接口调用失败的回调函数。
complete function - 否 接口调用结束的回调函数(调用成功、失败都会执行)。

错误码信息:

错误码 错误信息
101 writeFile:fail.
102 writeFile:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String.

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108

1. writeFile:fail the filePath is not writeable.

2. writeFile:fail the filePath is invalid.

90000701 writeFile:fail can not write data to the file.
90000702 writeFile:fail can not open file. It may not be a valid file.

示例:

const fileSystemManager = has.getFileSystemManager();
fileSystemManager.writeFile({
  filePath: 'internal://cache/demo.txt',
  data: '写入文本',
  encoding: 'utf-8',
  success: () => {
    console.info('writeFile success');
  },
  fail: (err) => {
    console.error('writeFile fail', err);
  },
  complete: (res) => {
    console.info('writeFile complete', res);
  }
});

FileSystemManager.writeFileSync

FileSystemManager.writeFileSync(string filePath, string|ArrayBuffer data, string encoding): void

同步写文件。

起始版本: 1.0.4

参数:

参数 类型 默认值 必填 描述
filePath string - 是 要写入的文件路径,仅支持internal://files/和internal://cache/类型。
data string | ArrayBuffer - 是 要写入的文本或二进制数据。
encoding string utf-8 否

只在data类型是string时有效,指定写入文件的字符编码。

在ASCF运行时2.0.5版本之前仅支持utf-8;

在ASCF运行时2.0.5及之后版本支持:ascii、base64、binary、hex、ucs2(ucs-2)、utf16le(utf-16le)、utf-8(utf8)、latin1。

错误码信息:

错误码 错误信息
101 writeFileSync:fail.
102 writeFileSync:fail encoding only support utf-8.
105

1. fail parameter error: path is empty.

2. fail parameter error: parameter.path should be String/

3. fail parameter error: Invalid path.

4. fail parameter error: path is too long.

108

1. writeFileSync:fail the filePath is not writeable.

2. writeFileSync:fail the filePath is invalid.

90000801 writeFileSync:fail can not write data to the file.
90000802 writeFileSync:fail can not open file. It may not be a valid file.

示例:

const fileSystemManager = has.getFileSystemManager();
try {
  fileSystemManager.writeFileSync('internal://cache/demo.txt', '写入文本', 'utf-8');
} catch (err) {
  console.error('writeFileSync error', err);
}