Lua IO文件copy

目录:
    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 name
function 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 flie
	io.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 flie
	f: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 stream
	read_file = io.open(sourcefile,"r")
	-- read all content
	temp_content = read_file:read("*a")


	-- open write file stream
	write_file = io.open(destinationfile,"w")


	-- write all content
	write_file:write(temp_content)
    -- close stream
	read_file:close()
	write_file:close()


end

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


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




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值