Skip to main content

Insert a sequence into file before a line (file.insert_lines)

Declaration​

ok, err = file.insert_lines(path, n, lines)

Parameters​

  • path
    String. Absolute file path.
  • n
    Integer. Line number. 0 means last line + 1. Negative means counting from the end.
  • lines
    Array. Lines to insert.

Returns​

  • ok
    Boolean. true if success, false if failure.
  • err
    String. Error message when operation fails.

Description​

Convert an array to lines and insert before the specified line. Creates file if it does not exist. Returns false if directory does not exist.
This function automatically strips UTF-8 BOM at the file head.

Example​

local success = file.insert_lines("/var/mobile/1.txt", 0, { -- append to the end
"The quick brown fox",
"jumps over the lazy dog",
})
if success then
sys.alert("Lines inserted successfully")
else
sys.alert("Failed")
end

Note: Uses sys.alert