Skip to main content

Insert content before a specific line (file.insert_line)

Declaration

ok, err = file.insert_line(path, n, content)

Parameters

  • path
    String. Absolute file path.
  • n
    Integer. Line number. 0 means last line + 1. Negative means counting from the end.
  • content
    String. Data to insert before the specified line.

Returns

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

Notes

Insert content before the specified line. If the number of lines is not enough, empty lines will be filled. 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, err = file.insert_line("/var/mobile/1.txt", 2, "岂因祸福避趋之")
if success then
sys.alert("Success")
else
sys.alert("Failed: "..err)
end

Note: Uses sys.alert