中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何逐行读取文件文本内容?
微信扫码分享
import { common } from '@kit.AbilityKit'; import fs from '@ohos.file.fs'; import { BusinessError } from '@kit.BasicServicesKit'; import { Options } from '@kit.CoreFileKit'; @Entry @Component struct Index { private context = getContext(this) as common.UIAbilityContext; build() { Row() { Column() { Text("Hello") .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { let applicationContext = this.context.getApplicationContext(); let cacheDir = applicationContext.cacheDir; let dirPath = cacheDir + "/aaa"; let options: Options = { encoding: 'utf-8' }; fs.open(dirPath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE).then((file: fs.File) => { fs.readLines(file.path,options, (err: BusinessError , readerIterator: fs.ReaderIterator) => { if (err) { console.error("read failed with error message: " + err.message + ", error code: " + err.code); } else { for (let it = readerIterator.next(); !it.done; it = readerIterator.next()) { console.info("content: " + it.value); } } }) }) }) } .width('100%') } .height('100%') } }