Fun program of reading and writing small files

Applet development, there will be some scenes need to use reading and writing files. In a previous project, the need to analyze the motion of the device in the hands of the holder, data and analysis on the device by gravity accelerometer and orientation sensor position and orientation information acquisition device, because too much data on real-time upload sure unrealistic, so he decided to write real-time file, after running for some time, go read the contents of the file, go to upload.

Official API  (really simple, who can read ah, a head manually save your life :))

Or line and bar myself

First, write a new file

The establishment of a global variable fsm. Parameter data that we want to write the contents of the file. filePath is critical, for if I did not write it, you simply can not find it where to go, or do not have permission to write. wx.env.USER_DATA_PATH micro channel allows the user to write to a file folder. \ N can be used to wrap.

let fsm = wx.getFileSystemManager();

fsm.writeFile({

  filePath: wx.env.USER_DATA_PATH + '/tmp.txt',

  data: "深蹲数据" + util.formatDate(new Date(), "yyyyMMdd_HHmmss") + "\n",

  encoding: 'utf8',

  success: res => {

    console.info(res)

  },

  fail: res => {

    console.info(res)

  }

})

Second, the additional content to file

If the acquisition had already file system manager fsm, here you only need enough. Similarly, data is to be appended to the contents of the file.

let fsm = wx.getFileSystemManager();

fsm.appendFile({

  filePath: wx.env.USER_DATA_PATH + '/tmp.txt',

  data: '[' + aX + ',' + aY + ',' + aZ + "],\n",

  encoding: 'utf8',

  success: res => {

    console.info(res)

  },

  fail: res => {

    console.info(res)

  }

});

Third, read the saved file

After the first reading in order to save the file, you can get the address of the temporary file after save, then use this address as an argument when reading. After reading you can do uploading logic, and here I omitted.

let fsm = wx.getFileSystemManager();

wx.saveFile({

tempFilePath: wx.env.USER_DATA_PATH + '/tmp.txt',

success(res) {

  fsm.readFile({

    filePath:res.savedFilePath,

    encoding: 'utf8',

    success:function(res){

      console.log(res.data)

    }

  })

}

})

Is not it simple? In fact, the entire file to read and write and append, just know where it is stored in the address to write well, but the official API, but no, not God amazing? Meaning no accident?

 

Codeword is not easy, if that helps, be sure to give me some praise yo ~ ~

Otherwise, believe it or not I smashed your house lights, midnight kiss you steal (¯ε ¯) !!!

Guess you like

Origin www.cnblogs.com/tonyccc/p/11470090.html