module(...,package.seeall)--гɼ

local USER_DIR_PATH = "/user_dir"

--[[demoṩֽӿڣһreadfile(filename)ļڶwritevala(filename,value)дļݣģʽ
function writevalw(filename,value)дļݣģʽdeletefile(filename)ɾļ--]]

--[[
    readfile(filename)
	ܣļļ
	ļ
	ֵ                     ]]
local function readfile(filename)--ָļ
	
    local filehandle=io.open(filename,"r")--һļڶǴ򿪷ʽ'r'ģʽ,'w'дģʽݽи,'a'ģʽ,'b'ģʽʾԶʽ
	if filehandle then          --жļǷ
	    local fileval=filehandle:read("*all")--ļ
	  if  fileval  then
	       print(fileval)  --ļڣӡļ
		   filehandle:close()--رļ
	  else 
	       print("ļΪ")--ļ
	  end
	else 
	    print("ļڻļʽȷ") --ʧ  
	end 
	
end



--[[
     writevala(filename,value)
	ܣļݣݸԭļ֮
	һļڶҪӵ
	ֵ                         --]]
local function writevala(filename,value)--ָļ,һλǴ򿪵ģʽ
	local filehandle = io.open(filename,"a+")--һļһǴģʽ'r'ģʽ,'w'дģʽݽи,'a'ģʽ,'b'ģʽʾԶʽ
	if filehandle then
	    filehandle:write(value)--дҪд
	    filehandle:close()
	else
	    print("ļڻļʽȷ") --ʧ  
	end
end



--[[
    writevalw(filename,value)
	ܣļݣӵݻḲǵԭļе
	ͬ
	ֵ                 --]]
local function writevalw(filename,value)--ָļ
	local filehandle = io.open(filename,"w")--һļһǴģʽ'r'ģʽ,'w'дģʽݽи,'a'ģʽ,'b'ģʽʾԶʽ
	if filehandle then
	    filehandle:write(value)--дҪд
	    filehandle:close()
	else
	    print("ļڻļʽȷ") --ʧ  
	end
end


--[[deletefile(filename)
    ܣɾָļе
	ļ
	ֵ             --]]
local function deletefile(filename)--ɾָļе
	local filehandle = io.open(filename,"w")
	if filehandle then
	    filehandle:write()--дյ
	    print("ɾɹ")
		filehandle:close()
	else
	    print("ļڻļʽȷ") --ʧ  
	end
end

--ӡļϵͳʣռ
print("get_fs_free_size: "..rtos.get_fs_free_size().." Bytes")
--ɹһĿ¼(Ŀ¼ѴڣҲtrueʾɹ)
if rtos.make_dir(USER_DIR_PATH) then
	readfile(USER_DIR_PATH.."/3.txt")

	writevala(USER_DIR_PATH.."/3.txt","great")

	readfile(USER_DIR_PATH.."/3.txt")
	writevalw(USER_DIR_PATH.."/3.txt","great")
	readfile(USER_DIR_PATH.."/3.txt")

	deletefile(USER_DIR_PATH.."/3.txt")
	readfile(USER_DIR_PATH.."/3.txt")
end
