w***@126.com
w***@126.com
  • 发布:2021-11-26 17:26
  • 更新:2023-03-14 16:37
  • 阅读:1953

在uniapp中使用Native.js 使用字节流读写文件工具类封装

分类:Native.js

/**

  • @description: gpb文件读写工具 采用Native.js

  • @author: Marco

  • @email: wtz_xupt@126.com
    /
    export default class GpbFileUtil {
    /
    *

    • 单例模式 获取实例
      */
      static getIntance(){
      if(!GpbFileUtil.instance){
      GpbFileUtil.instance = new GpbFileUtil()
      }

      return GpbFileUtil.instance
      }

    /**

    • 构造函数
      */
      constructor(){
      this.init()
      }

    /**

    • 初始化
      */
      init(){
      //导入类需要时间,统一导入提高效率
      this.main = plus.android.runtimeMainActivity()
      this.Environment = plus.android.importClass('android.os.Environment')
      this.File = plus.android.importClass('java.io.File')
      this.FileInputStream = plus.android.importClass("java.io.FileInputStream")
      this.FileOutputStream = plus.android.importClass('java.io.FileOutputStream')
      }

    /**

    • 判断有没有插入sdcard
      */
      checkSDCardExist(){
      if(this.Environment.getExternalStorageState() !== this.Environment.MEDIA_MOUNTED){
      return false
      }
      return true
      }

    /**

    • 获取sdcard根路径
      */
      getSDCardRoot(){
      return this.Environment.getExternalStorageDirectory()
      }

    /**

    • 判断sdcard 文件是否存在
    • @param {Object} filePath
    • @param {Object} fileName
      */
      checkSDCardFileExist(filePath,fileName){
      let fileFullPath = this.getSDCardRoot() + this.File.separator + filePath + this.File.separator + fileName
      let file = new this.File(fileFullPath)
      return file.exists()
      }

    /**

    • 删除sdcard中的文件

    • @param {Object} filePath 文件路径

    • @param {Object} fileName 文件名称
      */
      deleteSDCardFile(filePath,fileName){
      let fileFullPath = this.getSDCardRoot() + this.File.separator + filePath + this.File.separator + fileName

       let file = new this.File(fileFullPath)  
       if(file.exists()){  
      	 file.delete()  
       }  
      

    }

    /**

    • 读取sdcard中文件内容

    • @param {Object} filePath 文件路径

    • @param {Object} fileName 文件名称
      */
      readFileFromSDCard(filePath,fileName){
      if(!this.checkSDCardFileExist(filePath,fileName)){
      plus.nativeUI.toast(fileName + '文件不存在!')
      return;
      }
      let fileFullPath = this.getSDCardRoot() + this.File.separator + filePath + this.File.separator + fileName

      let file = new this.File(fileFullPath)

      let fis = new this.FileInputStream(file)

      let bytes = new Uint8Array(fis.available())
      try{
      let index = 0
      let byte
      while((byte = fis.read()) !== -1){
      bytes[index] = byte
      ++index
      }

      }catch(e){
      //TODO handle the exception
      console.log('读取sd文件' + fileName + '失败:' + e.message)
      }finally{
      try{
      if(fis !== null){
      fis.close()
      }
      }catch(ex){
      //TODO handle the exception
      console.log('关闭文件输入流失败:' + ex.message)
      }
      }

      return bytes
      }

    /**

    • 向sdcard中写文件
    • @param {Object} filePath 文件路径
    • @param {Object} fileName 文件名称
    • @param {Object} fileContentBytes 文件内容字节数组
      */
      writeFileToSDCard(filePath,fileName,fileContentBytes){
      this.deleteSDCardFile()
      let fileFullPath = this.getSDCardRoot() + this.File.separator + filePath + this.File.separator + fileName
      let file = new this.File(fileFullPath)
      let fos = new this.FileOutputStream(file)
      try{
      let index = 0
      while(index < fileContentBytes.length){
      fos.write(fileContentBytes[index])
      ++index
      }
      //fos.write(fileContentBytes) //java byte数组在nativejs中写入不了,只能循环一个一个字节写入
      fos.flush()
      }catch(e){
      //TODO handle the exception
      console.log('写入sd文件' + fileName + '失败:' + e.message)
      }finally{
      try{
      if(fos !== null){
      fos.close()
      }
      }catch(ex){
      //TODO handle the exception
      console.log('关闭文件输出流失败:' + ex.message)
      }
      }
      }

}

在测试过程中nativejs 调用java FileInputStream read(byte []) 和write(byte []) 读取和写入不了。尝试循环后每次读取或写入一个字节成功了。

0 关注 分享

要回复文章请先登录注册

1***@163.com

1***@163.com

为什么我读取出来是空的
2023-03-14 16:37
胡新涛

胡新涛

试了试,读取130多k的文件,卡了几分钟。而且转换循环一个一个读。字符流能按行读,勉强过得去
2022-06-07 17:13