harmony 鸿蒙Single I/O Task Development
Single I/O Task Development
Asynchronous concurrency provided by Promise and async/await is applicable to the development of a single I/O task. The following uses the asynchronous concurrency capability to write a file as an example.
Implement the logic of a single I/O task.
import fs from '@ohos.file.fs'; import { BusinessError } from '@ohos.base'; async function write(data: string, filePath: string): Promise<void> { let file: fs.File = await fs.open(filePath, fs.OpenMode.READ_WRITE); fs.write(file.fd, data).then((writeLen: number) => { fs.close(file); }).catch((err: BusinessError) => { console.error(`Failed to write data. Code is ${err.code}, message is ${err.message}`); }) }
Use the asynchronous capability to invoke the single I/O task. For details about how to obtain filePath in the example, see Obtaining Application File Paths.
let filePath: string = "path"; // Application file path write('Hello World!', filePath).then(() => { console.info('Succeeded in writing data.'); })
你可能感兴趣的鸿蒙文章
harmony 鸿蒙ArkTS Common Library
harmony 鸿蒙Comparison Between the Actor and Memory Sharing Models
harmony 鸿蒙Overview of ArkTS Common Library
harmony 鸿蒙Asynchronous Concurrency Overview
harmony 鸿蒙Concurrency Overview
harmony 鸿蒙Overview of Containers
harmony 鸿蒙CPU Intensive Task Development
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦