如何读写各种途径创建的文件

权限已经在json5文件中声明Read和write1.直接在沙箱目录里写文件,如test.txt,则成功2.如果在沙箱目录里用IDE或upload方式创建一个文件夹,则读写里面的文件都会失败3.如果代码在沙箱目录创建子文件夹,往子文件夹里写文件,第一次只能创建子文件夹,无法创建文件,第二次打开app才能成功创建文件并写入。

HarmonyOS
2024-06-13 23:41:19
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
jshyb

2.是正常现象,非bug,使用upload方式创建的文件,没有访问权限;

3.使用fs.open时fs.OpenMode.CREATE表示若文件不存在,则创建文件,不用再在前面添加判断。

注意fs.mkdir为异步接口可能文件夹没创建就写入文件导致写入失败,使用同步接口fs.mkdirSync(fileOnlyPath)可以解决该问题。

参考代码:

import { BusinessError } from '@kit.BasicServicesKit'; 
import fs from '@ohos.file.fs'; 
 
@Entry 
@Component 
struct Page { 
  build() { 
    Row() { 
      Column() { 
        Text("判断文件夹是否存在") 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            let pathDir = getContext().filesDir + "/testDir1/testDir2/testDir3"; 
            fsMkdirPath(pathDir) 
          }) 
        Text("写入文件") 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            let pathDir = getContext().filesDir + "/testDir1/testDir2/testDir3/test.txt"; 
            let file = fs.openSync(pathDir, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 
            let str: string = "hello, world"; 
            fs.write(file.fd, str).then((writeLen: number) => { 
              console.info("testTag- write data to file succeed and size is:" + writeLen); 
            }).catch((err: BusinessError) => { 
              console.error("testTag- write data to file failed with error message: " + err.message + ", error code: " + err.code); 
            }).finally(() => { 
              fs.closeSync(file); 
            }); 
          }) 
 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
 
function fsMkdirPath(filePath: string) { 
  fs.access(filePath).then((res: boolean) => { 
    if (res) { 
      console.info("testTag- file exists"); 
    } else { 
      console.info("testTag- file not exists"); 
      fs.mkdirSync(filePath, true) 
    } 
  }).catch((err: BusinessError) => { 
    console.error("access failed with error message: " + err.message + ", error code: " + err.code); 
  }); 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
分享
微博
QQ
微信
回复
2024-06-14 22:50:05


相关问题
如何以流形式读写文件
1598浏览 • 1回复 待解决
HarmonyOS 文件读写权限如何获得?
1533浏览 • 1回复 待解决
鸿蒙系统文件读写权限如何设置?
1954浏览 • 0回复 待解决
harmonyos怎么读写文件
12363浏览 • 1回复 已解决
HarmonyOS 文件读写问题
923浏览 • 1回复 待解决
HarmonyOS文件读写相关问题
1719浏览 • 1回复 待解决
HarmonyOS文件读写权限问题
1213浏览 • 1回复 待解决
程序怎么读写设备上文件啊?
4992浏览 • 1回复 待解决
HarmonyOS 文件读写在哪找
883浏览 • 1回复 待解决
HarmonyOS native层文件读写权限
836浏览 • 1回复 待解决
如何对一个文件进行读写操作
1290浏览 • 1回复 待解决
HarmonyOS 学习途径推荐
10563浏览 • 2回复 已解决
HarmonyOS 如何获取文件创建时间
543浏览 • 1回复 待解决
HarmonyOS 应用文件分享-读写权限咨询
1281浏览 • 1回复 待解决
HarmonyOS NAPI层怎么读写沙盒文件
850浏览 • 1回复 待解决
HarmonyOS应用内配置文件读写问题
1513浏览 • 1回复 待解决
HarmonyOS 如何创建文件
630浏览 • 1回复 待解决
HarmonyOS中各种context区分
509浏览 • 1回复 待解决