DEV Community

Cover image for HarmonyOS development: file operations in the hvigor plug-in
程序员一鸣
程序员一鸣

Posted on

HarmonyOS development: file operations in the hvigor plug-in

Foreword

this article API>= 13, based on DevEco Studio 5.1.0 Release, version number 5.1.0.828

in the previous article "hvigor plug-in Implements componentized operation", we generated a file, which is mainly used to control the switching between module running package and shared package. How was this file created? Of course, there are many application scenarios, for example, before building, check the code specification, if there is any non-compliance, output to an error file, or need to dynamically modify the code in the project, etc., how to implement the hvigor plug-in development? That's what this article is going to outline, file operations in the hvigor plugin.

There are two ways to operate files. One is to use fs module in node, which provides a large number of methods to read, write, modify, delete files and directories, and obtain file information. Using it, we can operate files. In addition, the government also provides us with Api,FileUtil tool class, for file operation.

Of course, the current FileUtil tool class only supports some basic file operations. Although there is no perfect fs module, it can basically meet our requirements for file operations.

Before using FileUtil, we first import the module, as shown in the following code:

import { FileUtil } from '@ohos/hvigor';
Enter fullscreen mode Exit fullscreen mode

overview of main methods

1, determine whether a file path exists

use the exist method to determine whether a file address exists, which can be a folder or a file, as shown in the following code.

In many scenarios, we need to use this method. For example, we need to judge before creating a file and before copying a file.

 const filePath = node.getNodePath() + "/test.json";
      if (FileUtil.exist(filePath)) {
        console.log("File exists");
      } else {
        console.log("File doesn't exist");
      }
Enter fullscreen mode Exit fullscreen mode

(2) Determine whether is the Directory

const filePath = node.getNodePath() + "/test.json";
      if (FileUtil.isDictionary(filePath)) {
        console.log("It is a directory");
      } else {
        console.log("It is not a directory");
      }
Enter fullscreen mode Exit fullscreen mode

3-Judgment whether it is a file

 const filePath = node.getNodePath() + "/test.json";
      if (FileUtil.isFile(filePath)) {
        console.log("It is a directory");
      } else {
        console.log("It is not a directory");
      }
Enter fullscreen mode Exit fullscreen mode

make sure the directory or file exists

in many scenarios, our requirement is to ensure that a directory or a file must exist and create it if it does not exist. According to the previous method, we will first determine whether the directory or file exists and create it if it does not exist. Generally, it takes two steps to implement it. However, in FileUtil, we can do it in one step.

Use the ensureDirSync method to ensure that the directory exists and is created if it does not exist; Use the ensureFileSync method to ensure that the file exists and is created if it does not exist.

The usage is very simple. For example, the file does not exist. After construction, one will be created directly.

Image description

5. Read the file

to read files, FileUtil provides three reading methods, the first is to read Json5 file readJson5() method, the second is to read file readFileSync() method synchronously, and the third is to read file readFile() method asynchronously.

The reading of json5 file will directly return json object to us, and we can directly operate the object, which is very convenient.

 const jsonContent = FileUtil.readJson5(filePath);
Enter fullscreen mode Exit fullscreen mode

It is relatively simple to read files synchronously. We can directly pass in a file address.

 const content = FileUtil.readFileSync(filePath);
Enter fullscreen mode Exit fullscreen mode

Read asynchronously, returns a Promise , We can use then to get asynchronously, of course, we can also use async and await.

const content = await FileUtil.readFile(filePath);
Enter fullscreen mode Exit fullscreen mode

6. Write a file

at present, the government has also given two ways to write files, one is synchronization and the other is to return Promise. The asynchronous form.

Synchronous writes:

 FileUtil.writeFileSync(filePath, "content")
Enter fullscreen mode Exit fullscreen mode

asynchronous writes:

FileUtil.writeFile(filePath, "content");
Enter fullscreen mode Exit fullscreen mode

whether synchronous or asynchronous, make sure that the file exists before writing the file, otherwise an error will be reported. You can run the FileUtil.ensureFileSync(filePath) function before writing the file to ensure that the file exists.

7. Copy files

at present, the government also provides two ways to copy files, namely synchronous replication and asynchronous replication.

Synchronous replication:

 FileUtil.copyFileSync(filePath, copyFilePath);
Enter fullscreen mode Exit fullscreen mode

asynchronous replication:

 FileUtil.copyFile(filePath, copyFilePath);
Enter fullscreen mode Exit fullscreen mode

related Summary

the methods provided by the FileUtil tool class are very simple. When the typescript project is developed, there may be errors, but don't worry, integrate DevEco Studio can be executed normally, but when writing, you can verify it in DevEco Studio first, and then copy it to typescript project to Type Plug-in package.

This article label: Hongmeng Development Tools/DevEco Studio

Top comments (0)