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.

Notes

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
"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("+1s")
else
sys.alert("Failed")
end

Note: Uses sys.alert