【发布时间】:2017-02-08 15:41:30
【问题描述】:
我正在尝试在我的 android 应用程序中实现导出和导入,我尝试了以下导出方法,但它不起作用并且没有给出任何错误。谁能帮帮我。
public static void export() throws IOException {
//Open your local db as the input stream
String inFileName = "/data/data/com.example.main/databases/myDB";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()+"/myDB";
//Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
//Close the streams
output.flush();
output.close();
fis.close();
}
我这样调用方法:
try {
export();
}
catch (IOException e) {
e.printStackTrace();
}
我做错了什么?并且一旦导出,我该如何导入数据?
【问题讨论】:
-
请详细解释“它不起作用”是什么意思。
-
我不确定到底是什么不工作,数据没有被导出。我在单击按钮时使用 export() 方法,所以当我按下按钮时什么都没有发生。如果我在没有提出的方法中有 Log.d。
-
你从哪里得到的代码?你听得懂么?您是否在日志中获得堆栈跟踪?
-
我没有得到堆栈跟踪。
-
您确定这就是您导出数据库的方式吗?您能否检查您正在使用
FileInputStream阅读的文件是否可以通过这种方式导出?在我看来,您需要通过某种方式与 SQL 进行通信才能提出此要求。
标签: java android sqlite android-studio export