lua 文件操作


lua 文件操作

         

                

                                 

文件操作

       

相关函数:相比于io操作,file可同时操作多个文件

file:close ():关闭文件
file:flush ():刷新文件,将写入内容刷新到文件中
file:write (···):向文件中写入内容

        

file.read(format):根据指定的格式读取文件内容

Reads the file file, according to the given formats, which specify what to read. 
For each format, the function returns a string or a number with the characters 
read, or fail if it cannot read data with the specified format. (In this latter 
case, the function does not read subsequent formats.) When called without 
arguments, it uses a default format that reads the next line (see below).
* 根据指定的格式读取文件内容
 
The available formats are
    "n": reads a numeral and returns it as a float or an integer, following the 
lexical conventions of Lua. (The numeral may have leading whitespaces and a sign.) 
This format always reads the longest input sequence that is a valid prefix for a 
numeral; if that prefix does not form a valid numeral (e.g., an empty string, "0x", 
or "3.4e-") or it is too long (more than 200 characters), it is discarded and the 
format returns fail.
    "a": reads the whole file, starting at the current position. On end of file, it 
returns the empty string; this format never fails.
    "l": reads the next line skipping the end of line, returning fail on end of 
file. This is the default format.
    "L": reads the next line keeping the end-of-line character (if present), 
returning fail on end of file.
    number: reads a string with up to this number of bytes, returning fail on end 
of file. If number is zero, it reads nothing and returns an empty string, or fail 
on end of file.
 * n:读取一个数字并返回它。例:file.read("n")
 * a:从当前位置读取整个文件。例:file.read("a")
 * l:读取下一行,跳过文件结束符,在文件尾 (EOF) 处返回 nil。例:file.read("l")
 * L:读取下一行,保留文件结束符,在文件尾 (EOF) 处返回 nil。例:file.read("L")
 * number:返回一个指定字符个数(字节单位)的字符串,或在 EOF 时返回 nil。例:file.read(5)
 
The formats "l" and "L" should be used only for text files. 
 * l、L只应该用在文本文件中

          

file:lines(format):根据指定的格式读取文件

Returns an iterator function that, each time it is called, reads 
the file according to the given formats. When no format is given, 
uses "l" as a default. 
* 根据指定的格式(默认l),遍历读取文件

for c in file:lines(1) do body end
* 遍历读取文件示例

As an example, the construction will iterate over all characters 
of the file, starting at the current position. Unlike io.lines, 
this function does not close the file when the loop ends. 
* 该函数从当前位置遍历读取文件,只到文件末尾
* 和io.lines不同,读到结尾时,file.lines不会关闭文件

        

file:seek(whence, offset):设置、获取文件的当前位置,whence、offset均可选

Sets and gets the file position, measured from the beginning of the file, to the 
position given by offset plus a base specified by the string whence, as follows:
    "set": base is position 0 (beginning of the file);
    "cur": base is current position;
    "end": base is end of file;
* 设置、获取文件位置,whence为基准位置,offset为偏移量
* whence可选值:set(文件开始位置)、cur(文件当前位置)、end(文件结尾)

In case of success, seek returns the final file position, measured in bytes from 
the beginning of the file. If seek fails, it returns fail, plus a string describing 
the error.
* 如果找到,seek函数返回文件的最终位置(以开始位置字节计数)
* 如果没有找到,返回fail,还有错误描述符

The default value for whence is "cur", and for offset is 0. Therefore, the call 
file:seek() returns the current file position, without changing it; the call 
file:seek("set") sets the position to the beginning of the file (and returns 0); 
and the call file:seek("end") sets the position to the end of the file, and returns 
its size. 
* whence默认为cur,offset默认为0
* file:seek("set"):返回文件开始位置,0
* file:seek():返回文件当前位置
* file:seek("end"):返回文件大小

          

file:setvbuf (mode, size):为文件设置缓存模式

Sets the buffering mode for a file. There are three available modes:
    "no": no buffering.
    "full": full buffering.
    "line": line buffering.
* 为文件设置缓存模式,可选值:no(无缓存)、full(全文件缓存)、line(行缓存)

For the last two cases, size is a hint for the size of the buffer, 
in bytes. The default is an appropriate size.
* full、line这两种模式,缓存大小以字节表示,默认是一个合适的大小

The specific behavior of each mode is non portable; check the underlying 
ISO C function setvbuf in your platform for more details. 
* 每种模式是不兼容的,可检查一下系统的c函数查看详情

       

           

                                 

使用示例

       

示例:file:read

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 打开文件file2、file3
> file=io.open("file2")
> file2=io.open("file3")

-- 读取file的一行数据
> file:read("l")
瓜田李下
> file:read("l")
瓜田李下2

-- 读取file的一行数据,包含分行符
> file:read("L")
瓜田李下3

-- 读取file2的一行数据
> file2:read()
海贼王

-- 读取file2的一行数据,包含分行符
> file2:read("L")
海贼王2

-- 读取file2的纯数字,没有读到
> file2:read("n")
nil

-- 从当前位置读取全部内容
> file2:read("a")
海贼王3
海贼王4
海贼王5
海贼王6

-- 继续读取file2的内容,由于已经读完,返回nil
> file2:read("L")
nil

         

示例:file:read("n")

huli@hudeMacBook-Pro ~ % cat file4
2
323
2322

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> file=io.open("file4")
> file:read("n")
2
> file:read("n")
323
> file:read("n")
2322
> file:read("n")
nil
> file:read("n")
nil
> file:read("n")
nil

        

示例:file.read("n")

huli@hudeMacBook-Pro ~ % cat file4
2
323
瓜田李下
2322

-- file:read("n"):读取连续的数字行,如不连续,后续都返回nil
huli@hudeMacBook-Pro ~ % lua      
Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> file=io.open("file4")
> file:read("n")
2
> file:read("n")
323
> file:read("n")
nil
> file:read("n")
nil
> file:read("n")
nil
> file:read("n")
nil
> file:read("n")
nil

         

示例:file.write

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 以写入模式打开文件
> file=io.open("file5","w")

-- 以追加模式打开文件
> file2=io.open("file6","a")

-- 写入内容
> file:write("瓜田李下\n")
file (0x7ff8462c8880)
> file:write("海贼王\n")
file (0x7ff8462c8880)
> file2:write("海贼王\n")
file (0x7ff8462c8918)
> file2:write("瓜田李下\n")
file (0x7ff8462c8918)

-- 将写入内容刷新到文件
> file:flush()
true
> file2:flush()
true

-- 查看写入内容 
huli@hudeMacBook-Pro ~ % cat file5
瓜田李下
海贼王
huli@hudeMacBook-Pro ~ % cat file6
瓜田李下2海贼王2海贼王
瓜田李下

       

示例:file:lines

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> file=io.open("file6")

--遍历读取行数据
> for value in file:lines("l") do print(value) end
瓜田李下2海贼王2海贼王
瓜田李下

-- 在当前位置继续读取,数据已经读完,没有数据
> for value in file:lines("L") do print(value) end
> 
huli@hudeMacBook-Pro ~ % lua


Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> file=io.open("file6")

-- 遍历读取行数据,包含分行符
> for value in file:lines("L") do print(value) end
瓜田李下2海贼王2海贼王

瓜田李下

> 

        

                

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值