Lua IO文件copy

来源:互联网 发布:mt4交易软件下载 编辑:程序博客网 时间:2024/06/06 04:33
目录:
    1.普通文件读写
    2.二进制文件读写
    3.普通文件copy
    4.二进制文件copy
    5.文件读写模式分类
    6.io.read函数的模式


1.普通文件读写
    1.1 文件读
    
-- function:read_common_file("C:\\Users\\whx\\Desktop\\demo.txt")    -- filename(string):file namefunction read_common_file(filename)local f = "";local content = "";f = io.input(filename)content = io.read("*a")print(content)end 

    1.2 文件写
-- function:write_common_file("C:\\Users\\whx\\Desktop\\demo1.txt")    -- filename(string):file name    function write_common_file(filename)local f = "";local content = "";f = io.output(filename)io.write("Hello,I am Andy_Lau")-- clear flush and to write in flieio.flush()io.close()end
    
2.二进制文件读写
    2.1 文件读
-- function:read_file("C:\\Users\\whx\\Desktop\\demo1.txt")      -- filename(string):file name      function read_file(filename)local f = "";local content = "";f = io.open(filename,"r")content = f:read("*a")print(content)end

    2.2 文件写 
-- function:write_file("C:\\Users\\whx\\Desktop\\demo1.txt","a+")       -- filename(string):file name -- mode(string):r+ a a+ w w+   function write_file(filename,mode)local f = "";local content = "";f = io.open(filename,mode)f:write(",new string")-- clear flush and to write in flief:flush()    f:close()end
  
3.普通文件copy
-- function:common_copy("C:\\Users\\whx\\Desktop\\demo.txt","C:\\Users\\whx\\Desktop\\demo1.txt")      -- sourcefile(string):source file -- destinationfile(string):destination file function common_copy(sourcefile,destinationfile)local temp_content ="";io.input(sourcefile)temp_content = io.read("*a")io.output(destinationfile)io.write(temp_content)io.flush()io.close()end

4.二进制文件copy
-- function:stream_copy("C:\\Users\\whx\\Desktop\\demo.txt","C:\\Users\\whx\\Desktop\\demo1.txt")      -- sourcefile(string):source file -- destinationfile(string):destination file function stream_copy(sourcefile,destinationfile)local read_file =""local write_file=""local temp_content ="";-- open read file streamread_file = io.open(sourcefile,"r")-- read all contenttemp_content = read_file:read("*a")-- open write file streamwrite_file = io.open(destinationfile,"w")-- write all contentwrite_file:write(temp_content)    -- close streamread_file:close()write_file:close()end

5.文件读写模式分类
    r:  读模式
    w:  写模式,之前数据全覆盖
    a:  追加模式,追加到文档尾部
    r+:更新模式,覆盖掉原数据的开始部分
    w+:更新模式,之前数据全覆盖
    a+:追加模式,数据保留只允许在文件尾部写入


6.io.read函数的模式 
    *n:读取数字
    *a:读取整个文件
    *l(默认):读取下一行
    number:返回一个指定个数的字符串




    
0 0
原创粉丝点击