Java 导入/导出Sqlite Db Android期间的文件名
在我的应用程序中,我使用了导入和导出备份功能,但每次都会因为静态路径而将其替换为旧文件?那么我该如何解决这个问题呢 -->在SD卡上导出Java 导入/导出Sqlite Db Android期间的文件名,java,android,Java,Android,在我的应用程序中,我使用了导入和导出备份功能,但每次都会因为静态路径而将其替换为旧文件?那么我该如何解决这个问题呢 -->在SD卡上导出 public void exportDB(){ try { File sd = Environment.getExternalStorageDirectory(); if (sd.canWrite()) { String currentDBPath = "data/data/com.exampl
public void exportDB(){
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = "data/data/com.example.mybudget/databases/MyBudget.db";
String backupDBPath = sd + "/MyBudget.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(backupDBPath);
if(sd.exists())
{
Toast.makeText(MainActivity.this, "Backup Exists Woult you like to Replace ?", 1000).show();
}
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
----->从SD卡导入
public void importDB(){
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = sd + "/MyBudget.db";
String backupDBPath = "data/data/com.example.mybudget/databases/MyBudget.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
可以将日期添加到文件名,如下所示:
private static final String DATE_FORMAT_NOW = "yyyyMMddHHmmss";
private String now() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
(...)
String backupDBPath = sd + "/MyBudget-"+now()+".db";
要检索最后一个文件,请在按日期(从新到旧)或名称(因为日期在名称中)对文件进行排序时仅获取第一个文件。静态路径是什么意思?存储在字符串变量string path=“sdcard/MyBudget.db”中的路径;不相关的标题,只是在导入导出期间被要求重命名文件。你们知道如何在listview中保持相同大小的数据吗?这是另一个问题,问一个新问题。谢谢,但是什么时候进口呢?对不起,我听不懂你说的。首先谢谢你的帮助。通过此操作,我可以创建备份文件的新实例。但假设当我创建一个帐户名home_数据时,我现在导出数据库,然后删除数据库。我正在创建一个新的帐户办公室\ U数据,现在我再次导出数据。但这次它只导入我第一次导出的文件。它将导入home_数据备份文件,但如果我想获取office_数据备份文件,该怎么办?