安卓上找不到路径这个问题暂时用sd卡的根目录好啦。这里已经修复并测试通过
但这个测试工程是安装在有sd卡的手机的sd卡上的
或者如阳光七月所说
将Lua的源代码用Quick提供的工具打包,用加载包的方式进行代码的加载工作,就不会出现问题了。
如果不愿意打包,也可以自己修改executeScriptFile接口,先自己载入文件数据,用luaL_dostring接口来调用。这样也行
------------dataSave.lua--------------
local function readInFiles(filePath, str, sType)
local result = {}
– 把字符串写入文件
local function writeFile(fileName, strVal)
if device.platform == “android” then
local tab = string.split(fileName, “/”)
local tmpPath = “/mnt/sdcard/”
fileName = tmpPath … tab#tab]
end
local pFile,err = assert(io.open(fileName, sType))
if err then
return false
else
pFile:write(strVal)
pFile:close()
return true
end
end
return writeFile(filePath, str)
end
–打印输出信息
function showLog( … )
local traceback = string.split(debug.traceback("", 2), “\n”)
local str = string.split(string.trim(traceback), “/”)
print(str#str] … “\n”, …)
end
– 格式化table表
local function forMatTable(object, nesting)
if type(nesting) ~= “number” then nesting = 99 end
local lookupTable = {}
local result = {}
local function _v(v)
if type(v) == “string” then
v = “”" … v … “”"
end
return tostring(v)
end
local function _forMat(object, label, indent, nest, keylen)
spc = “”
if type(keylen) == “number” then
spc = string.rep(" “, keylen - string.len(_v(label)))
end
if type(object) ~= “table” then
if label then
– 格式化每一行数据
result#result +1 ] = string.format(”%s%s]%s = %s,", indent, _v(label), spc, _v(object))
else
result#result +1 ] = string.format("%s,", _v(object))
end
elseif lookupTable then
– result#result +1 ] = string.format("%s%s%s = REF", indent, label, spc)
else
lookupTable = true
if nest > nesting then
– result#result +1 ] = string.format("%s%s = MAX NESTING", indent, label)
else
if label then
– 格式化每一行数据
result#result +1 ] = string.format("%s%s] = {", indent, _v(label))
else
result#result +1 ] = “{”
end
local indent2 = indent…" "
local keys = {}
local keylen = 0
local values = {}
for k, v in pairs(object) do
keys#keys + 1] = k
local vk = _v(k)
local vkl = string.len(vk)
if vkl > keylen then keylen = vkl end
values = v
end
table.sort(keys, function(a, b)
if type(a) == “number” and type(b) == “number” then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
_forMat(values, k, indent2, nest + 1, keylen)
end
result#result +1] = string.format("%s},", indent)
end
end
end
_forMat(object, nil, “”, 1)
local tb = table.concat(result, “\n”)
return string.sub(tb,1,string.len(tb)-1)
end
function varForMat(object)
local lookupTable = {}
local result = {}
local function _v(v)
if type(v) == “string” then
v = “”" … v … “”"
end
return tostring(v)
end
local function _vardump(object, label, indent, nest)
local postfix = “”
if nest > 1 then postfix = “,” end
if type(object) ~= “table” then
if type(label) == “string” then
result#result +1] = string.format("%s%s = %s%s", indent, label, _v(object), postfix)
else
result#result +1] = string.format("%s%s%s", indent, _v(object), postfix)
end
elseif not lookupTable then
lookupTable = true
if label then
if type(label) == “string” then
result#result +1 ] = string.format("%s%s = {", indent, label)
else
result#result +1 ] = string.format("%s{", indent)
end
else
result#result +1 ] = string.format("%s{", indent)
end
local indent2 = indent … " "
local keys = {}
local values = {}
for k, v in pairs(object) do
keys#keys + 1] = k
values = v
end
table.sort(keys, function(a, b)
if type(a) == “number” and type(b) == “number” then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
_vardump(values, k, indent2, nest + 1)
end
result#result +1] = string.format("%s}%s", indent, postfix)
end
end
_vardump(object, nil, “”, 1)
return table.concat(result, “\n”)
end
– 获取文件路径
local function getDataTablePath(fileName)
fileName = tostring(fileName)
local path = CCFileUtils:sharedFileUtils():getWritablePath()
if device.platform == “android” then
path = “/mnt/sdcard/”
end
local filePath = path … fileName … “.lua”
return filePath
end
– 判断文件是否存在
local function isDataTableExist(fileName)
local filePath = getDataTablePath(fileName)
return CCFileUtils:sharedFileUtils():isFileExist(filePath)
end
–
以下是需要暴露出去的接口
–]]
– 保存数据
function saveDataTableToFile(tab, fileName)
local fStr =
local table = %s
return table
]]
sType = sType or ‘w’
local filePath = getDataTablePath(fileName)
tab = forMatTable(tab)
–tab = varForMat(tab)
local str = string.format(fStr, tab)
if readInFiles(filePath, str, sType) then
–showLog(“数据保存到 “,filePath,” 操作成功”)
end
end
– 读取数据
function getDataTableFromFile(fileName)
local filePath = getDataTablePath(fileName)
if isDataTableExist(fileName) then
if device.platform == “android” then
local tmpPath = “/mnt/sdcard/”
fileName = tmpPath … fileName … “.lua”
end
local info = require(fileName)
return info
else
showLog(“读取失败,未找到: “,filePath,” 该文件”)
return {}
end
end
– 删除数据
function deleteDataTable(fileName)
local filePath = getDataTablePath(fileName)
if isDataTableExist(fileName) then
os.remove(filePath)
else
showLog(“删除失败,未找到: “,filePath,” 该文件”)
end
end
– 测试
– local t = {{x=2, b=“right”}, {2, 6,8}, {name = “哈哈”, sex = “女”}}
– 保存数据
– saveDataTableToFile(t, “tip”)
– 读取数据
– local a = getDataFromFile(“tip”)
– print(a.b)