收藏
回答

FileSystemManager模块write接口position参数有bug

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug FileSystemManager.write 工具 wechat_devtools_1.06.2307260_win32_x64 微信开发者工具调试基础库版本号 2.33.0以及3.0.0

data的类型为string时,设定position的值为非Number类型,如string类型的'12'、‘ab’、boolean类型的true和false、object类型{'name':'test'}时,都会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变。比如原有的数据为0123456789,写入data的内容为abcde,position设为true,则结果为abced6789。同时,data的类型为ArrayBuffer时,写入文件后,使用readFileSync读取文件的输出是乱码的,不能正确直观地呈现数据。测试代码如下:

write() {
    const fs = wx.getFileSystemManager()
    const ab = new ArrayBuffer(1024)
    const abSmall = new ArrayBuffer(10)
    let dataView = new DataView(ab)
    const value = 123456789
    dataView.setInt32(0, value)
    console.log(dataView.getInt32(0))
    // 打开文件
    const fd=fs.openSync({filePath:`${wx.env.USER_DATA_PATH}/hello.txt`,flag:'r+'})
    // 写入文件
    fs.write({
      fd:fd,
      data:'0123456789abcdef',
      encoding:'utf8',
      /** position  指定文件开头的偏移量,即数据要被写入的位置
       * 打开模式flag为'a+'时,position取任意值都视为无效,都会指向文件末尾,但'r+'模式下有效
       * 【 1 合法 】 10 从指定位置开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 1.1 num {bytesWritten: 16, errMsg: "write:ok"}
       * 【 2 非法 】 
       * 2.1 '20'  会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 2.2 '' 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 2.3 'ab'  会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 2.4 true  会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 2.5 false  会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 2.6 null  会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 2.7 object  会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       * 2.8 undefined  会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变
       */
      position:true,
      success(res) {
        console.log(res)
        console.log('The write bytes: '+res.bytesWritten)
      },
      fail(err){
        console.error(err)
      }
    })
      // 读取文件内容
      const readRes = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`'utf8')
      console.log('The result of read file: ' + readRes)
  },
data的类型为string时,设定position的值为非Number类型,如string类型的'12'、‘ab’、boolean类型的true和false、object类型{'name':'test'}时,都会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变。比如原有的数据为0123456789,写入data的内容为abcde,position设为true,则结果为abced6789。测试代码如下: write() { const fs = wx.getFileSystemManager() const ab = new ArrayBuffer(1024) const abSmall = new ArrayBuffer(10) let dataView = new DataView(ab) const value = 123456789 dataView.setInt32(0, value) console.log(dataView.getInt32(0)) // 打开文件 const fd=fs.openSync({filePath:`${wx.env.USER_DATA_PATH}/hello.txt`,flag:'r+'}) // 写入文件 fs.write({ fd:fd, data:'0123456789abcdef', encoding:'utf8', /** position 指定文件开头的偏移量,即数据要被写入的位置 * 打开模式flag为'a+'时,position取任意值都视为无效,都会指向文件末尾,但'r+'模式下有效 * 【 1 合法 】 10 从指定位置开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 1.1 num {bytesWritten: 16, errMsg: "write:ok"} * 【 2 非法 】 * 2.1 '20' 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 2.2 '' 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 2.3 'ab' 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 2.4 true 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 2.5 false 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 2.6 null 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 2.7 object 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 * 2.8 undefined 会从头开始写,然后把原有数据覆盖掉,覆盖的长度为写入数据的长度,之后的内容不变 */ position:true, success(res) { console.log(res) console.log('The write bytes: '+res.bytesWritten) }, fail(err){ console.error(err) } }) // 读取文件内容 const readRes = fs.readFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`, 'utf8') console.log('The result of read file: ' + readRes) },
回答关注问题邀请回答
收藏
登录 后发表内容