Skip to main content

Set a specific line in a text file (file.set_line)

Declaration

ok, err = file.set_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 set to the specified line.

Returns

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

Notes

Set the content of a specific 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.set_line("/var/mobile/1.txt", 3, "哈哈哈")
if success then
sys.alert("Success")
else
sys.alert("Failed: "..err)
end

Note: Uses sys.alert