HarmonyOS应用内配置文件读写问题

请问项目中需要预置一些配置文件,在HarmonyOS应用中,这种配置文件放在哪里?如何对其进行读写?

HarmonyOS
2024-09-09 11:22:49
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以将需要的配置文件放到工程的moduleName/src/main/resources/rawfile/{需要的文件名} ,文件名也可以写死;文件内容可以设置成key=value形式。下述方法是简单的demo,方法中并未约束文件名,可以根据需要进行调整:

import { Context } from '@ohos.abilityAccessCtrl'; 
import hilog from '@ohos.hilog'; 
import util from '@ohos.util'; 
const TAG: string = 'testTag' 
export class Nd { 
  public _id?: string; 
  public _name?: string; 
  public _other?: string; 
  constructor(id?: string, name?: string, other?: string) { 
    this._id = id; 
    this._name = name; 
    this._other = other; 
  } 
  public set id(id: string) { 
    this._id = id 
  } 
  public set name(name: string) { 
    this._name = name 
  } 
  public set other(other: string) { 
    this._other = other 
  } 
} 
export class FileUtils { 
  public static async getFileContent(context: Context, filePath: string): Promise<Nd | undefined> { 
    try { 
      hilog.info(0x00000, TAG, 'har包读取资源文件'); 
      let data = await context.resourceManager.getRawFileContent(filePath) 
 
      if (!data) { 
        hilog.error(0x00000, TAG, '读取的数据为空'); 
        return undefined 
      } else { 
        hilog.info(0x00000, TAG, 'data:' + data.byteLength); 
      } 
      let uc = util.TextDecoder.create() 
      let result = uc.decodeWithStream(data) 
      hilog.info(0x00000, TAG, '转string后:' + result); 
      let proArr: string[]; 
      if (result.includes('\r\n')) { 
        proArr = result.split('\r\n') 
      } else { 
        proArr = result.split('\n') 
      } 
      hilog.info(0x00000, TAG, 'proArr数组长度:' + proArr.length); 
      let nd = new Nd() 
      for (let i = 0; i < proArr.length; i++) { 
        if (!proArr[i].includes('=')) { 
          continue; 
        } 
        let oneProArr = proArr[i].split('=') 
        if (oneProArr.length != 2) { 
          continue; 
        } 
        let oneProKey = oneProArr[0].trim() 
        let oneProValue = oneProArr[1].trim() 
        switch (oneProKey) { 
          case 'id': 
            nd.id = oneProValue 
            break; 
          case 'name': 
            nd.name = oneProValue 
            break; 
          case 'other': 
            nd.other = oneProValue
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
分享
微博
QQ
微信
回复
2024-09-09 17:35:25


相关问题
HarmonyOS 文件读写问题
616浏览 • 1回复 待解决
HarmonyOS 配置文件中metadata如何读取?
1063浏览 • 1回复 待解决
DevEco Device Tool配置文件在哪?
7174浏览 • 1回复 待解决
HarmonyOS文件读写权限问题
933浏览 • 1回复 待解决
配置文件的文档的问题有懂的吗?
3672浏览 • 1回复 待解决
HarmonyOS文件读写相关问题
1311浏览 • 1回复 待解决
配置文件配置权限时报错如下
1984浏览 • 1回复 待解决
关于权限申请的配置文件声明
2237浏览 • 1回复 待解决
关于 web 项目配置文件是否使用缓存
4467浏览 • 1回复 待解决
HarmonyOS 应用文件分享-读写权限咨询
1008浏览 • 1回复 待解决
HarmonyOS 应用支付问题
969浏览 • 1回复 待解决
module.json5配置文件中type的含义
1118浏览 • 1回复 待解决