当前位置: 首页 > wzjs >正文

ppt网站网站设计与网页制作

ppt网站,网站设计与网页制作,wordpress多站点无法发布文章,中国网站的建设ohos/hvigor FileUtil用法 在鸿蒙(HarmonyOS)开发中,ohos/hvigor 的 FileUtil 是用于文件操作的实用工具类,提供了跨平台的文件读写、路径处理等常用方法。以下是其核心用法和示例: 一、核心方法说明 方法名功能描…

@ohos/hvigor FileUtil用法
 

在鸿蒙(HarmonyOS)开发中,@ohos/hvigor 的 FileUtil 是用于文件操作的实用工具类,提供了跨平台的文件读写、路径处理等常用方法。以下是其核心用法和示例:


一、核心方法说明

方法名功能描述-----------------------------------------------------------------------------------------------------------

|  readFile(path: string) | 异步读取文件内容(返回 Promise<string>) |

|  writeFile(path: string, content: string) | 异步写入文件内容(自动创建目录) |   

|  copyFile(src: string, dest: string) | 异步复制文件(支持覆盖) |    

 deleteFile(path: string) | 异步删除文件 |  | 

 mkdirs(dirPath: string) | 递归创建目录 |  

exists(path: string) | 检查文件或目录是否存在(返回 Promise<boolean>) |

listFiles(dirPath: string)| 列出目录下所有文件(返回 Promise<string[]>) |

normalizePath(path: string) | 标准化路径(跨平台处理路径分隔符) |


二、基础用法示例

1. 读取文件内容
import { FileUtil } from '@ohos/hvigor';async function readConfig() {const content = await FileUtil.readFile('src/main/resources/config.json');console.log('配置文件内容:', content);
}
2. 写入文件
async function generateVersionFile() {const version = '1.0.0';await FileUtil.writeFile('build/version.txt', `Version: ${version}`);
}
3. 复制资源文件
async function copyAssets() {await FileUtil.copyFile('assets/raw/icon.png', 'build/res/icon.png');
}
4. 递归创建目录
async function prepareDirs() {await FileUtil.mkdirs('build/generated/code');
}

三、实战场景

场景 1:动态生成配置文件
import { FileUtil } from '@ohos/hvigor';class ConfigGeneratorPlugin implements HvigorPlugin {apply(context: HvigorContext) {context.taskRegistry.registerTask('generateConfig', async () => {const config = {apiUrl: 'https://api.example.com',debug: context.getBuildType() === 'debug'};await FileUtil.writeFile('build/generated/config.json', JSON.stringify(config, null, 2));});}
}
场景 2:清理构建产物
async function cleanBuild() {const files = await FileUtil.listFiles('build');for (const file of files) {await FileUtil.deleteFile(file);}
}

四、路径处理技巧

结合 FileUtil.normalizePath 处理跨平台路径:

const winPath = 'src\\main\\resources';
const unixPath = FileUtil.normalizePath(winPath); // 转换为 'src/main/resources'

五、错误处理

async function safeRead(path: string) {try {if (await FileUtil.exists(path)) {return await FileUtil.readFile(path);}return '';} catch (err) {console.error('文件操作失败:', err.message);throw err;}
}

六、与 Node.js fs 模块对比

| 特性 | FileUtil | Node.js fs | |---------------------|-------------------------------------|--------------------------------| 

API 风格 | Promise-based | Callback/Promise/Sync 混合 | 

| 目录自动创建 | 写文件时自动创建父目录 | 需要手动处理 | 

| 跨平台路径 | 内置 normalizePath | 需依赖 path 模块 | 

鸿蒙集成 | 深度集成 Hvigor 构建流程 | 通用文件操作 |


七、注意事项

  1. 路径基准:相对路径基于 模块根目录(含 hvigorfile.ts 的目录)
  2. 性能优化:批量操作建议使用 listFiles + 循环处理
  3. 权限问题:确保对目标目录有写权限

通过 FileUtil 可以更便捷地实现构建流程中的文件操作,建议结合 Hvigor 官方文档 深入使用。


文章转载自:

http://I5N4kxVS.pLwfx.cn
http://Ar6gDJar.pLwfx.cn
http://ZjOHFoL2.pLwfx.cn
http://agM2f4H8.pLwfx.cn
http://Mf7o1CNW.pLwfx.cn
http://kVpQ6DDe.pLwfx.cn
http://0TbhCIZY.pLwfx.cn
http://10oziP0J.pLwfx.cn
http://WkzsUHeb.pLwfx.cn
http://S5saT1wl.pLwfx.cn
http://vyEWkOXt.pLwfx.cn
http://5KTe5oOe.pLwfx.cn
http://SSV2hKaD.pLwfx.cn
http://zPnwdetG.pLwfx.cn
http://qw8AZkRv.pLwfx.cn
http://6hNxStox.pLwfx.cn
http://SnbLgRSO.pLwfx.cn
http://DPZbG3I7.pLwfx.cn
http://Lso6pVvo.pLwfx.cn
http://sy4VomHY.pLwfx.cn
http://rYB0Ijxr.pLwfx.cn
http://5qceZy9i.pLwfx.cn
http://ELtSZYmJ.pLwfx.cn
http://NVaJpbO2.pLwfx.cn
http://5GC388Rg.pLwfx.cn
http://6o868pbn.pLwfx.cn
http://yw8S7azO.pLwfx.cn
http://To6dnbGQ.pLwfx.cn
http://RFYKH1ng.pLwfx.cn
http://4knfxiuW.pLwfx.cn
http://www.dtcms.com/wzjs/623717.html

相关文章:

  • 违法网站开发者学编程哪家机构最好
  • 网站定制那个好有没有专业做挂的网站吗
  • 网站建设的要求有哪些方面360seo排名优化服务
  • 国外做网站卖东西要什么条件让别人做网站需要注意什么
  • 怎么做网站申请广告摄影比赛投稿网站
  • 网站制作公司信科网络本科电子商务专业就业方向
  • 深圳企业推广网站秒赞网站建设
  • 网站排名点击工具中信建设有限责任公司 吴方旭
  • 小型网站建设教程判断网站cms
  • 理财网站方案建设php网站整合dz论坛
  • 网站开发电子商务网上做设计兼职哪个网站好点
  • 宠物网站开发与实现结论石狮网站建设联系电话
  • 平板做网站服务器什么是自助网站
  • 石家庄市城乡和建设局网站做机械外贸什么网站好
  • 做网站公司电话wordpress文件上传位置
  • 深圳网站建设服务哪便宜西湖区高端网站建设
  • 贵阳微网站怎么做网站设计方案
  • 合肥最好的网站建设公司网站域名被注销
  • 自己建设网站用哪个基础网站建设的实施步骤
  • 优化网站流量担路网如何快速做网站
  • 网站建设创新互联公司深圳做网站哪家便宜
  • 福州城市建设规划网站淘宝运营推广
  • 个人网站备案和企业网站备案吗手机网站模板 网址
  • 甘肃路桥建设集团网站WORDpress文章分类栏目
  • 旅游景点网站建设方案网站改版 需求文档
  • php网站里放asp在建项目人员查询网站
  • 东莞中堂网站建设深圳网站维护优化
  • 中国做外贸的网站网站编程薪资
  • 阿里云中英文网站建设地推推广平台
  • app优化网站开发简单代码制作