This article mainly introduces the Lua file read and write detailed, this article explained the file reads and writes the simple model and the complete model, and has given an operation example, needs the friend to be possible to refer to under
The file read-write model in Lua comes from the C language and is divided into complete models (like C) and simple models.
1. Simple model
Io.input ([file]) sets the default input file, file name (read in text) or file handle (can be understood as a handle, can find the file with handle), return the document handle.
Io.output ([file]) sets the default output file, the parameter is the same as above.
Io.close ([file]) closes the file and closes the default file with no parameters
Io.read (formats) reads the default file, formats takes the value of "*a" (Read All), "*n" (read by number), "*l" (read by row, default), N (that is, number, read n characters).
Io.lines ([fn]) FN filename, if no file, take default file, return an iterator, can be used in the For loop.
Io.write (value) writes content to the default file.
Io.flush () Acts immediately to the default output file for operations in the file cache.
Examples in the last.
2. Complete model
In a simple model, only text-type files can be processed, and binary files are processed in the full model.
The general process for processing files is: Open the file, get the handle to the file, manipulate the file with the file handle, and close the file.
You can see that the full model is more complex than the simple model, but the advantage is that it is more powerful.
Io.open (FN [, M]) opens the file, returns the file handle, FN filename, and M mode has:
R opens the file as read-only and the file must exist.
W Open Write-only file, if the file exists, the file length is 0, that is, the file content will disappear. If the file does not exist, the file is created.
A opens the write-only file in an additional way. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, that is, the original contents of the file will be retained. (The EOF character is reserved)
r+ opens the file in a read-write manner, and the file must exist.
w+ open a writable file, if the file exists, the file length is zero, that is, the file content will disappear. If the file does not exist, the file is created.
A + is similar to a, but this file can be read and writable
b binary mode, if the file is a binary file, you can add B
The + number indicates that the file can be read or written
The following file is a handle to the files returned by Io.open, similar in functionality to the simple schema, and is no longer detailed.
File:seek ([P] [, of]) sets the offset for file reads and writes, p file offset starting position (the value has "set", the file header, this is the default value, "cur" current position, "end" file tail), the offset (default value 0, positive representation forward, negative representation backwards), Returns the new current position in the file.
Copy code code as follows:
Local File1=io.input ("1.txt")--current directory "1.txt" to exist, or error
Local Str=io.read ("*a")
Local File2=io.output ("2.txt")--The current directory "2.txt" does not need to exist
Use these functions to do a file copy function
function Copy (Filea,fileb)
Local File1=io.input (Filea)
If not file1 then print (Filea ...) Not present ") return end
Local Str=io.read ("*a")
Local File2=io.output (Fileb)
For line in Io.lines ("1.txt") do
Local F=io.open ("3.txt", "A +")
F:write ("Happy New year!")
F:seek ("End",-1)--navigates to the previous byte of the file
Local Str=f:read (1)--read a character
Print (str)-Output "!"