分享在项目中用到的一个用于管理缓存文件的模块
2011-10-04 22:48
766 查看
Corona的lua源代码文件是不支持存放在子目录下的,目前我也没有找到在Document目录下创建子目录的方法,可以说corona在文件管理方面非常不方便。下面的代码的作用是
1)记录App从网络上下载的文件列表,防止重新下载。
2)清空列表内的缓存文件
主要的原理是,每次下载文件,都把文件名或者对应的ID写入到文件中(数据以JSON的格式保存),清空的时候,先去读取列表文件,根据里面记录的文件名和ID来删除文件。
--[[
模块名称:物品缓存文件管理[itemsFileMgr.lua]
功能说明:
对存储物品信息的缓存文件进行管理
创建者:XXX
版本更新:
[2011-08-06]创建了第一个版本
--]]
module(..., package.seeall)
local json = require "json"
local hasReadTab = false
local data = {} --存储文件列表
local filename = "items.json"
--读取数据文件
function readData()
local path = system.pathForFile( filename, system.DocumentsDirectory )
local wfh = io.open(path, "r")
local ret = ""
if wfh then
ret = wfh:read( "*a" )
wfh:close()
end
--print("ret", ret)
return ret
end
--添加新的物品ID(在下载新的物品时候后使用)
function addID( id, pic)
if not hasRead then
local s = readData()
if string.len(s) > 0 then
data = json.decode(s)
end
end
if not data.ids then data.ids = {} end
if not data.pics then data.pics = {} end
if id then data.ids[#data.ids + 1] = id end
if pic then data.pics[#data.pics + 1] = pic end
local jsonData = json.encode(data)
local path = system.pathForFile( filename, system.DocumentsDirectory )
local wfh = io.open(path, "w+")
if wfh then
wfh:write( jsonData )
wfh:flush()
wfh:close()
end
end
--清空所有的物品信息缓存文件
function clear()
local destDir = system.DocumentsDirectory
local OSremove = os.remove
if not hasRead then
local s = readData()
if string.len(s) > 0 then
data = json.decode(s)
else
viewAlert.show("緩存已全部清空")
end
end
if data.ids then
for k, v in ipairs(data.ids) do
OSremove(system.pathForFile("item" .. tostring(v) .. ".txt", destDir))
end
end
if data.pics then
for k, v in ipairs(data.pics) do
OSremove(system.pathForFile(tostring(v) .. ".png", destDir))
end
end
common.clearTable(data)
local path = system.pathForFile( filename, system.DocumentsDirectory )
local wfh = io.open(path, "w+")
if wfh then
wfh:write( "" )
wfh:flush()
wfh:close()
end
--OSremove(system.pathForFile(filename, destDir))
end
http://www.buildapp.net/post/130.htm
1)记录App从网络上下载的文件列表,防止重新下载。
2)清空列表内的缓存文件
主要的原理是,每次下载文件,都把文件名或者对应的ID写入到文件中(数据以JSON的格式保存),清空的时候,先去读取列表文件,根据里面记录的文件名和ID来删除文件。
--[[
模块名称:物品缓存文件管理[itemsFileMgr.lua]
功能说明:
对存储物品信息的缓存文件进行管理
创建者:XXX
版本更新:
[2011-08-06]创建了第一个版本
--]]
module(..., package.seeall)
local json = require "json"
local hasReadTab = false
local data = {} --存储文件列表
local filename = "items.json"
--读取数据文件
function readData()
local path = system.pathForFile( filename, system.DocumentsDirectory )
local wfh = io.open(path, "r")
local ret = ""
if wfh then
ret = wfh:read( "*a" )
wfh:close()
end
--print("ret", ret)
return ret
end
--添加新的物品ID(在下载新的物品时候后使用)
function addID( id, pic)
if not hasRead then
local s = readData()
if string.len(s) > 0 then
data = json.decode(s)
end
end
if not data.ids then data.ids = {} end
if not data.pics then data.pics = {} end
if id then data.ids[#data.ids + 1] = id end
if pic then data.pics[#data.pics + 1] = pic end
local jsonData = json.encode(data)
local path = system.pathForFile( filename, system.DocumentsDirectory )
local wfh = io.open(path, "w+")
if wfh then
wfh:write( jsonData )
wfh:flush()
wfh:close()
end
end
--清空所有的物品信息缓存文件
function clear()
local destDir = system.DocumentsDirectory
local OSremove = os.remove
if not hasRead then
local s = readData()
if string.len(s) > 0 then
data = json.decode(s)
else
viewAlert.show("緩存已全部清空")
end
end
if data.ids then
for k, v in ipairs(data.ids) do
OSremove(system.pathForFile("item" .. tostring(v) .. ".txt", destDir))
end
end
if data.pics then
for k, v in ipairs(data.pics) do
OSremove(system.pathForFile(tostring(v) .. ".png", destDir))
end
end
common.clearTable(data)
local path = system.pathForFile( filename, system.DocumentsDirectory )
local wfh = io.open(path, "w+")
if wfh then
wfh:write( "" )
wfh:flush()
wfh:close()
end
--OSremove(system.pathForFile(filename, destDir))
end
http://www.buildapp.net/post/130.htm
相关文章推荐
- 5年以前开发一个消费场所会员管理软件的开发经验分享,小项目一般人折腾不起,靠小项目比较难创业成功
- 给大家分享“河南郑州政府投资代建工程建设管理监督中心-GOVPM网欣政府代建工程项目管理信息化解决方案.pdf”文件
- 使用Jquery+EasyUI进行框架项目开发案例解说之中的一个---员工管理源代码分享
- 使用Jquery+EasyUI进行框架项目开发案例解说之中的一个---员工管理源代码分享
- 分享一个大一的课程设计,学校新闻系统——新闻管理模块
- SPORE,一个很好的实现JS命名空间的管理,模块的定义,与自动加载依赖模块的脚本文件
- 以前vs03项目中用到一个类:读写config文件配置信息
- CUDA项目文件管理之-一个源文件
- 使用Jquery+EasyUI进行框架项目开发案例解说之中的一个---员工管理源代码分享
- 使用Jquery+EasyUI进行框架项目开发案例解说之中的一个---员工管理源代码分享
- 一个NFS缓存管理包的bug导致文件系统满的问题和解决方法
- 在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)
- 使用Jquery+EasyUI进行框架项目开发案例解说之中的一个---员工管理源代码分享
- XConfigGen——这是一个基于nodejs,用于生成不同环境项目配置文件的小工具
- 分享一个项目中用到的简单的id生成工具
- [Unix/Linux] catsrc.sh: 此脚本用于在写报告时把项目的多个源代码文件方便的 `cat` 到一个文件上, 并把来自不同的文件之间加上一个标识头以区分输出文件里的不同文件
- 在Web.Config文件中使用configSource,避免动态修改web.config导致asp.net重启(另添加一个Config文件用于管理用户数据)
- 使用Jquery+EasyUI 进行框架项目开发案例讲解之五--模块(菜单)管理源码分享
- [iOS]Xcode清除证书、项目编译缓存、SVN版本管理隐藏文件
- 5年以前开发一个消费场所会员管理软件的开发经验分享,小项目一般人折腾不起,靠小项目比较难创业成功