Lua file read and write detailed

Source: Internet
Author: User
Tags file copy flush lua

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 (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. (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, similar in functionality to the simple schema, and is no longer detailed.

File:close ()

File:read (formats)

File:lines ()

File:write (values)

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.

File:flush ()

3, example

Copy code code as follows:

------------------Simple Model-----------------


Local File1=io.input ("1.txt")--current directory "1.txt" to exist, or error

Local ("*a")

Print (str)


Local File2=io.output ("2.txt")--The current directory "2.txt" does not need to exist

Io.write (str)

Io.flush ()

Io.close ()

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 ("*a")

Local File2=io.output (Fileb)

Io.write (str)

Io.flush ()

Io.close ()


For line in Io.lines ("1.txt") do

Print (line)


------------------Full Model-----------------

Local ("3.txt", "A +")

F:write ("Happy New year!")

F:flush ()

F:seek ("End",-1)--navigates to the previous byte of the file

Local Str=f:read (1)--read a character

Print (str)-Output "!"

F:close ()

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.