跳至主要內容

將一個順序表轉換逐行插入到檔案指定行前 (file.insert_lines)

聲明

寫入成敗, 錯誤資訊 = file.insert_lines(檔案路徑, 行號, 行陣列)

參數

  • 檔案路徑
    文字型,檔案絕對路徑
  • 行號
    整數型,指定行號,0 為最後一行 +1,負數則為倒數行號
  • 行陣列
    順序表型,需要轉換插入到檔案的表

回傳值

  • 寫入成敗
    布林型,操作成功返回 true,操作失敗返回 false
  • 錯誤資訊
    文字型,操作失敗時的錯誤資訊

說明

將一個順序表轉換逐行插入到檔案指定行前,檔案不存在會建立檔案,目錄不存在會返回 false
該函式會自動剔除掉檔案頭部的 UTF8-BOM

範例

local success = file.insert_lines("/var/mobile/1.txt", 0, { -- 將下面兩行字追加到文件末尾
"I will do whatever it takes to serve my country even at the cost of my own life,",
"regardless of fortune or misfortune to myself.",
})
if success then
sys.alert("操作成功")
else
sys.alert("操作失败")
end

:上述代碼中使用了非本章函式 sys.alert