当前位置:   article > 正文

【HarmonyOS】【ARKUI】ets怎么实现文件操作_harmonyos应用开发读取本地txt

harmonyos应用开发读取本地txt

 ets怎么实现文件操作?

关于文件操作的我们可以学习HarmonyOS文件管理Ability上下文 这两篇文档,我这边实现”文件路径读取”、“文件写入”“文件读取”,“运行效果”四个方面实现,具体操作如下

1.       文件路径读取

参考context.getFilesDir来进行获取文件路径,代码如下

  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. 文件写入

参考fileio.openSync的api,实现代码如下

  1. private writeFiles(){
  2. let fd = fileio.openSync(this.path+"/111.txt", 0o102, 0o666);
  3. fileio.write(fd, "你好 2022", function (err, bytesWritten) {
  4. if (!err) {
  5. console.log("写入成功")
  6. }
  7. });
  8. }

3.       文件读取

参考 fileio.read这个api ,代码如下

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

4.       运行效果

全部代码如下

  1. import fileio from '@ohos.fileio';
  2. import ability_featureAbility from '@ohos.ability.featureAbility';
  3. @Entry
  4. @Component
  5. struct NewmyFileTwo {
  6. @State path:string="";
  7. private getCacheDir(){
  8. var context = ability_featureAbility.getContext();
  9. context.getFilesDir()
  10. .then((data) => {
  11. console.log('File directory obtained. Data:' + data);
  12. this.path=data;
  13. }).catch((error) => {
  14. console.error('Failed to obtain the file directory. Cause: ' + error.message);
  15. })
  16. }
  17. private writeFiles(){
  18. let fd = fileio.openSync(this.path+"/111.txt", 0o102, 0o666);
  19. fileio.write(fd, "你好 2022", function (err, bytesWritten) {
  20. if (!err) {
  21. console.log("写入成功")
  22. }
  23. });
  24. }
  25. private ReadFile(){
  26. let Filepath = this.path+"/111.txt";
  27. let fd = fileio.openSync(Filepath, 0o2);
  28. let buf = new ArrayBuffer(4096);
  29. fileio.read(fd, buf, function (err, readOut) {
  30. if (!err) {
  31. let encodedString = String.fromCodePoint.apply(null, new Uint8Array(readOut.buffer));
  32. let decodedString = decodeURIComponent(escape(encodedString));//没有这一步中文会乱码
  33. console.log("读取文件内容"+decodedString);
  34. }
  35. });
  36. }
  37. private getFilesDirNew(){
  38. var context = ability_featureAbility.getContext();
  39. context.getFilesDir()
  40. .then((data) => {
  41. console.log('File directory obtained. Data:' + data);
  42. this.path=data;
  43. }).catch((error) => {
  44. console.error('Failed to obtain the file directory. Cause: ' + error.message);
  45. })
  46. }
  47. build() {
  48. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  49. Text('获取文件目录')
  50. .fontSize(50)
  51. .fontWeight(FontWeight.Bold)
  52. .onClick(this.getFilesDirNew.bind(this));
  53. Text('写文件 你好 2022 到文件中')
  54. .fontSize(50)
  55. .fontWeight(FontWeight.Bold)
  56. .backgroundColor(Color.Red)
  57. .onClick(this.writeFiles.bind(this));
  58. Text('读文件内容')
  59. .fontSize(50)
  60. .fontWeight(FontWeight.Bold)
  61. .backgroundColor(Color.White)
  62. .onClick(this.ReadFile.bind(this));
  63. }
  64. .width('100%')
  65. .height('100%')
  66. }
  67. }

效果图如下:

更多相关学习资料:
https://developer.huawei.com/consumer/cn/forum/topic/0202773634874690369?fid=0101591351254000314?ha_source=zzh 

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

闽ICP备14008679号