跳到正文
本頁目錄

📂 目錄與文件 API

下面這些API提供了操作文件、目錄等與文件系統互動的接口

注:所有傳入函數的相對路徑都以BDS根目錄為基準。

創建文件夾

File.createDir(dir)
File.mkdir(dir)

  • 參數:
  • dir : String
    目標文件夾的路徑
    可以直接創建多層,不需要逐層創建
  • 返回值:是否成功創建
  • 返回值類型:Boolean

刪除文件 / 文件夾

File.delete(path)

  • 參數:
  • path : String
    目標文件 / 文件夾的路徑
  • 返回值:是否成功刪除
  • 返回值類型:Boolean

判斷文件 / 文件夾是否存在

File.exists(path)

  • 參數:
  • path : String
    目標文件 / 文件夾的路徑
  • 返回值:目標是否存在
  • 返回值類型:Boolean

複製文件 / 文件夾到指定位置

File.copy(from,to)

  • 參數:

  • from : String
    源文件 / 文件夾的路徑

  • to : String
    目標文件 / 文件夾的位置

  • 返回值:是否複製成功

  • 返回值類型:Boolean

移動文件 / 文件夾到指定位置

File.move(from,to)

  • 參數:

  • from : String
    源文件 / 文件夾的路徑

  • to : String
    目標文件 / 文件夾的位置

  • 返回值:是否複製成功

  • 返回值類型:Boolean

重命名指定文件 / 文件夾

File.rename(from,to)

  • 參數:

  • from : String
    文件 / 文件夾的舊名字

  • to : String
    新名字

  • 返回值:是否複製成功

  • 返回值類型:Boolean

獲取指定文件的大小

File.getFileSize(path)

  • 參數:

  • path : String
    所操作的文件路徑

  • 返回值:文件的大小(字節)

  • 返回值類型:Integer

如果傳入的路徑位置是一個文件夾,則返回-1

判斷指定路徑是否是文件夾

File.checkIsDir(path)

  • 參數:
  • path : String
    所判斷的路徑
  • 返回值:目標路徑是否是文件夾
  • 返回值類型:Boolean

如果目標路徑不存在,同樣將返回false

列出指定文件夾下的所有文件 / 文件夾

File.getFilesList(dir)

  • 參數:

  • dir: String
    文件夾路徑

  • 返回值:文件名、文件夾名數組

  • 返回值類型:Array<String,String,...>