Android DB 파일 있는 경우 복사 하기



Android DB 파일이 있는 경우에 대해서 파일 복사 되게 설정


파일 호출 하는 부분


File file = new File("/data/data/kr.co.member/databases/DataBase.db");

if (SSMApplication.DEBUG_MODE) {
Log.i(this.getClass().getName(),"checkIO file check : "+ file.isFile());
}

// if db file is not exists execute
if (!file.isFile()) {


copyDbFile();

  }

DB 파일 복사 하는 부분
> db 파일은 asserts 하위에 있다. SSM_TEMP.db

public void copyDbFile() {

AssetManager am = mContext.getAssets();


File directory = new File(Environment.getDataDirectory()+"/data/kr.co.member/databases");

if (!directory.exists()) {
directory.mkdirs();
if (SSMApplication.DEBUG_MODE) {
Log.i(this.getClass().getName(),"directory make : " + directory);
}
}

File toFile = new File(Environment.getDataDirectory()+"/data/kr.co.member/databases/DataBase.db");

FileOutputStream fos = null;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;

try {
// checkFile("SSM_TEMP.db", "SSM_TEMP.db", db);
// File is = new File(db,"SSM_TEMP.db");

InputStream is = am.open("SSM_TEMP.db");

// InputStream bis = new FileInputStream(is);
bis = new BufferedInputStream(is);

// 만약에 파일이 있다면 지우고 다시 생성
if (toFile.exists()) {
toFile.delete();
toFile.createNewFile();
} else {
toFile.createNewFile();
}
fos = new FileOutputStream(toFile);
bos = new BufferedOutputStream(fos);

int read = -1;
byte[] buffer = new byte[1024];
while ((read = bis.read(buffer, 0, 1024)) != -1) {
bos.write(buffer, 0, read);
}
bos.flush();

fos.close();
bos.close();
bis.close();

} catch (IOException e) {
e.printStackTrace();
}
}

댓글

이 블로그의 인기 게시물

CoreOS Vagrant on Windows.md

The Docker User Guide

Install Docker