【毛豆工具集】【文件】【目录操作】生成沙盒目录

文件操作是App研发过程常见的操作,通常不同业务功能中的文件会放到不同的目录中,在不同的业务功能中进行管理,下面的代码示例实现了在沙盒文件中生成子目录的能力,支持指定多级子目录。

javascript 复制代码
function getRootDirPath(context: Context,subDir:string): string|null {
  const rootDirPath: string = context.filesDir + subDir
  let rootDirExist = false
  try {
    rootDirExist = fs.accessSync(rootDirPath);
    if (rootDirExist) {
      return rootDirPath;
    } else {
      fs.mkdirSync(rootDirPath, true);
      rootDirExist = fs.accessSync(rootDirPath);
      if (rootDirExist) {
        return rootDirPath;
      } else {
        return null;
      }
    }
  } catch (err) {
    return null
  }
  return rootDirPath;
}

使用代码示例

javascript 复制代码
let dirPath = getRootDirPath(context,"/maodou/image/");  //可以是多级目录
if (dirPath) {
    let fileName = dirPath + "xxx.png"
}
相关推荐
Csvn6 小时前
WASM
前端
修己xj7 小时前
我用 AI 给自己写了一个 Markdown 编辑器,然后把它开源了
前端
FRing037 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
幸运小猿8 小时前
模拟慢上传-处理超时重试image 丢失线上问题
前端·javascript·html
moshuying9 小时前
网购能买Token了!
前端
h_65432109 小时前
echarts实现柱状图加一条横线
前端·echarts
风骏时光牛马9 小时前
生成一个后端标题,只需要一个标题,不需要其他的任何文本。
前端
子非鱼@Itfuture9 小时前
关于前后端联调中登录状态/登录信息失效?
java·前端
郭小李9 小时前
openFeign入门
java·开发语言·前端
馒头不想说话10 小时前
AI当运营,帮你省人力
前端