FileUtils.ets 797 Bytes

import fs from '@ohos.file.fs';
import { BusinessError } from '@kit.BasicServicesKit';

export class FileUtils {

  // 文件是否存在,忽略错误
  static fileExsit(path: string) : Promise<boolean> {
    return new Promise((reslove, fail) => {
      fs.stat(path).then(() => {
        reslove(true)
      }).catch((error: BusinessError) => {
        if (error.code = 13900002) {
          reslove(false)
        } else {
          reslove(true)
        }
      })
    });
  }

  static makeDirIfNotExsit(path: string) : Promise<void> {
    return new Promise((reslove, fail) => {
      fs.stat(path).then(() => {
        reslove()
      }).catch((error: BusinessError) => {
        if (error.code = 13900002) {
          fs.mkdirSync(path)
        }
        reslove()
      })
    })
  }
}