기기의 일부 파일에 사용자 데이터를 저장하는 게임을하고 있습니다. 게임이 충돌 한 후 사용자는 이러한 데이터를 검색하지 않습니다. 충돌 직후 해당 파일이 비어 있습니다. 또한 아래에 백업 파일이 만들어져 새 데이터가 저장되므로 이전 데이터가 삭제되지 않습니다. 이 백업 파일도 비어 있습니다.Android - 충돌 후 파일이 비어졌습니다.
나는 어떻게 비워 질 수 있는지 이해하지 못한다. 당신은 저장 기능을 검색 할 수 있습니다
알고리즘 형태 :
copy file to file_backup
if save data to file_temp then
move file_temp to file
else
delete file_temp
C++ 형태 : 당신이 어떤 생각을 가지고있는 경우
const std::string path = "dataPlayer.dat";
const std::string backupPath = atPath + "_back";
if (PlatformInterface::fileExists(path))
{
if (PlatformInterface::fileExists(backupPath))
PlatformInterface::removeFile(backupPath);
PlatformInterface::copyFile(path, backupPath);
}
const std::string tempPath = path + "_temp";
// Save in compact form.
XMLError error = dataDocument.SaveFile(tempPath.c_str(), true);
if (error != XML_NO_ERROR)
{
PlatformInterface::removeFile(tempPath);
}
else
{
bool moved = PlatformInterface::moveFile(tempPath, path);
if (!moved)
{
PlatformInterface::removeFile(tempPath);
}
}
내가 당신을 감사합니다!
편집 : PlatformInterface :: CopyFile 수 (경로, backupPath) 방법 :
public static void copyFile(final String path, final String destination) {
try {
if (!new File(path).exists())
return;
InputStream in = new FileInputStream(path);
OutputStream out = new FileOutputStream(destination);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
파일이 제대로 닫혀있는 것 같다.
중요한 것은 : 왜 충돌이 발생하고이를 방지 할 수 있습니까? – Opiatefuchs
크래시가 앱 오류로 인한 것일 수 있지만 장치 종료 (배터리 부족, 중요한 장치 오류) 때문일 수 있습니다. – flchaux