我知道这是旧的,但链接上的代码的实际问题(除了布赖恩伯顿博士已经提到的)是他们没有关闭初始打开的文件以防万一。所以而不是:
if( file == nil )then
-- Doesn't Already Exist, So Copy it In From Resource Directory
pathSource = system.pathForFile( dbName, system.ResourceDirectory )
fileSource = io.open( pathSource, "r" )
contentsSource = fileSource:read( "*a" )
-- Write Destination File in Documents Directory
pathDest = system.pathForFile( dbName, system.DocumentsDirectory )
fileDest = io.open( pathDest, "w" )
fileDest:write( contentsSource )
-- Done
io.close( fileSource )
io.close( fileDest )
end
您应该在末尾添加和 ELSE 子句,如下所示:
if( file == nil )then
-- Doesn't Already Exist, So Copy it In From Resource Directory
pathSource = system.pathForFile( dbName, system.ResourceDirectory )
fileSource = io.open( pathSource, "rb" )
contentsSource = fileSource:read( "*a" )
-- Write Destination File in Documents Directory
pathDest = system.pathForFile( dbName, system.DocumentsDirectory )
fileDest = io.open( pathDest, "wb" )
fileDest:write( contentsSource )
-- Doneb
io.close( fileSource )
io.close( fileDest )
else
io.close(file)
end
干杯!