收藏
回答

调用getFileSystemManager写文件时报错?

代码如下:

                       fs.access({
                            path`${wx.env.USER_DATA_PATH}/log`,  //寻找以log为名称的文件夹
                            success(res) {
                                // 文件存在
                                console.log(res)
                                fs.access({
                                    path`${wx.env.USER_DATA_PATH}/log/` + newDate,  //寻找以日期为名称的文件夹
                                    success(res) {
                                        // 文件存在
                                        console.log(res)
                                        ////////////////////////////////////////
                                        fs.open({  //打开文件
                                            filePath`${wx.env.USER_DATA_PATH}/log/` + newDate + '/log.txt',    //打开文件路径
                                            flag'a+'//追加写入文件,如果没有文件,则创建该文件
                                            success(res) {
                                                // 写入文件
                                                fs.write({
                                                    fd: res.fd,
                                                    data: writebuf,
                                                    success(res) {
                                                        console.log(res)
                                                        console.log("写入数据成功")
                                                    },
                                                    fail(res) {
                                                        console.error(res)
                                                        console.log("写入数据失败")
                                                    },
                                                    complete(res) {
                                                        fs.close({
                                                            fd: res.fd
                                                        })
                                                        console.log("要写入的数据为:" + writebuf)
                                                    }
                                                })
                                            }
                                        })
                                        ///////////////////////////////////////////
                                    },
                                    fail(res) {  //没有找到该文件夹,创建
                                        fs.mkdir({
                                            dirPath`${wx.env.USER_DATA_PATH}/log/` + newDate,  //创建以日期为名称的文件夹
                                            success(res) {
                                                // 创建成功
                                                console.log(res)
                                            },
                                            fail(res) {
                                                console.error(res)
                                            }
                                        })
                                    }
                                })
                            },
                            fail(res) {  //没有找到该文件夹,创建
                                fs.mkdir({
                                    dirPath`${wx.env.USER_DATA_PATH}/log`,  //创建以log为名称的文件夹
                                    success(res) {
                                        // 文件存在
                                        console.log(res)
                                    },
                                    fail(res) {
                                        console.error(res)
                                    }
                                })
                            }
                        })
                    }
                }

想创建一个/log/日期的文件夹,在这个文件夹下创建log.txt文件,每10s写一次数据,刚开始运行写数据正常,运行了一段时间后报错,报错内容如下:

麻烦问一下各位大佬,这个是哪里出错了?谢谢!

最后一次编辑于  2022-08-19
回答关注问题邀请回答
收藏

1 个回答

  • 旬贵
    旬贵
    2022-08-20


    先创建文件并写入数据:FileSystemManager.writeFile(Object object)

    判断文件存在后,追加数据就行了:FileSystemManager.appendFile(Object object)


    2022-08-20
    有用
    回复 1
    • 大鹏
      大鹏
      发表于移动端
      2022-08-20
      我试一下,谢谢大佬了!
      2022-08-20
      回复
登录 后发表内容