当前位置:   article > 正文

【Harmony OS】【ARK UI】ets实现文件读写操作_arkui读写文件api

arkui读写文件api

 1.准备阶段

关于该功能的实现我们需要学习以下的资料:

1.1【ARKUI】ets怎么实现文件操作
1.2 文件管理
1.3 Ability上下文

2.demo实现
2.1 文件路径读取
参考context.getFilesDir来进行获取文件路径,代码如下

cke_2425.png

  1. private getCacheDir(){
  2. var context = ability_featureAbility.getContext();
  3. context.getFilesDir()
  4. .then((data) => {
  5. console.log('File directory obtained. Data:' + data);
  6. this.path=data;
  7. }).catch((error) => {
  8. console.error('Failed to obtain the file directory. Cause: ' + error.message);
  9. })
  10. }

2.2文件写入操作
参考fileio.createStream和write和flush相关Api,资料和代码如下

cke_3665.png

cke_4398.png

cke_5295.png

实现代码

  1. private writeFile(){
  2. let ss= fileio.createStreamSync(this.path+"/111.txt", "w+");
  3. let num = ss.write("你好 2022",null);
  4. ss.flush();
  5. console.log("写入成功")
  6. }

2.3文件读取文件操作
想实现文件的读取,需要参考read的Api,资料和代码如下:

cke_8540.png

代码如下:

  1. private readFile(){
  2. let ss = fileio.createStreamSync(this.path+"/111.txt", "r+");
  3. ss.read(new ArrayBuffer(4096),null,function (err, readOut) {
  4. if (!err) {
  5. let encodedString = String.fromCodePoint.apply(null, new Uint8Array(readOut.buffer));
  6. let decodedString = decodeURIComponent(escape(encodedString));//没有这一步中文会乱码
  7. console.log("读取文件内容:"+decodedString);
  8. }
  9. });
  10. }

3.运行效果

3.1全部代码如下

  1. import fileio from '@ohos.fileio';
  2. import ability_featureAbility from '@ohos.ability.featureAbility';
  3. @Entry
  4. @Component
  5. struct MyFileStream {
  6. @State path:string="";
  7. private getFilesDirNew(){
  8. var context = ability_featureAbility.getContext();
  9. context.getFilesDir()
  10. .then((data) => {
  11. console.log('获取文件路径成功' + data);
  12. this.path=data;
  13. }).catch((error) => {
  14. console.error('Failed to obtain the file directory. Cause: ' + error.message);
  15. })
  16. }
  17. private writeFile(){
  18. let ss= fileio.createStreamSync(this.path+"/111.txt", "w+");
  19. let num = ss.write("你好 2022",null);
  20. ss.flush();
  21. console.log("写入成功")
  22. }
  23. private readFile(){
  24. let ss = fileio.createStreamSync(this.path+"/111.txt", "r+");
  25. ss.read(new ArrayBuffer(4096),null,function (err, readOut) {
  26. if (!err) {
  27. let encodedString = String.fromCodePoint.apply(null, new Uint8Array(readOut.buffer));
  28. let decodedString = decodeURIComponent(escape(encodedString));//没有这一步中文会乱码
  29. console.log("读取文件内容:"+decodedString);
  30. }
  31. });
  32. }
  33. build() {
  34. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  35. Text('获取文件路径')
  36. .fontSize(50)
  37. .fontWeight(FontWeight.Bold)
  38. .backgroundColor(Color.Red)
  39. .onClick(this.getFilesDirNew.bind(this))
  40. Text('写入文字')
  41. .fontSize(50)
  42. .fontWeight(FontWeight.Bold)
  43. .backgroundColor(Color.White)
  44. .onClick(this.writeFile.bind(this))
  45. Text('读取文字')
  46. .fontSize(50)
  47. .fontWeight(FontWeight.Bold)
  48. .backgroundColor(Color.Red)
  49. .onClick(this.readFile.bind(this))
  50. }
  51. .width('100%')
  52. .height('100%')
  53. }
  54. }

3.2运行效果如下

cke_18957.png

 

欲了解更多更全技术文章,欢迎访问:https://developer.huawei.com/consumer/cn/forum/topic/0204809455827330196?fid=0102683795438680754?ha_source=zzh

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/255135
推荐阅读
相关标签
  

闽ICP备14008679号