|
|
测试文件名为:ip.txt 解压到模拟器目录路径 /mnt/sdcard/Pictures/ 下. 也可以自行创建txt文件.
测试文件下载:
ip.zip
(260 Bytes, 下载次数: 51)
执行一下代码:
- FILETAB = {} -- 全局变量
- function GetFileData(filepath)
- -- 获取本地文本文件路径,用于存储至表里
- local number = 1
- for line in io.lines(filepath) do
- if line == nil then
- print("文件数据错误")
- return false
- end
- print(line,'\n')
- FILETAB[number] = line
- number = number + 1
- end
-
- return true
-
- end
- function SaveFileData(filepath)
- -- 保存读完剩余数据
- io.output(filepath)
- for k,v in pairs(FILETAB) do
- io.write(v.."\n")
- end
- io.close()
-
- print("文件保存完毕",filepath)
-
- end
- function ReadOneDeleteOne(count)
- -- 读一行删除一行
- -- count 读取多少行
- local newtab = {}
- if count > #FILETAB then
- print("读取的行数超出")
- exitScript()
-
- else
- for i=1,count do
- print(FILETAB[i],'\n')
- end
- for i=(count+1),#FILETAB do
- if i <=#FILETAB then
- table.insert(newtab,FILETAB[i])
- end
- end
- FILETAB = newtab
- end
-
-
- end
- function run()
- -- 执行函数
- local filepath = "/mnt/sdcard/Pictures/ip.txt"
- if GetFileData(filepath) then
- print("源文件行数:",FILETAB)
- ReadOneDeleteOne(10)
- print("读取删除后行数",FILETAB)
- SaveFileData(filepath)
- end
- end
- run()
复制代码 运行结果图片:
|
|