尝试保存文件时未找到java FileNotFound
我有一个奇怪的问题。我正在尝试保存sqlite数据库的备份。我已经创建了这个代码
String DB_PATH;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
}
else {
DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + "/databases/";
}
String currentDBPath = "portals.db";
String backupDBPath = "backupname.db";
File sd = new File(Environment.DIRECTORY_DOWNLOADS + "/" + backupDBPath);
File data = new File(DB_PATH + currentDBPath);
try {
InputStream in = new FileInputStream(data);
OutputStream out = new FileOutputStream(sd);
// 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) {
int test = 1;
e.printStackTrace();
}
运行此命令时,我收到FileNotFound异常。它不应该创建文件吗?我甚至尝试创建一个虚拟文件,但结果仍然相同
请注意,我想将其保存在内存中,因为我的设备中没有SD卡。那个代码有什么问题
# 1 楼答案
我找到了解决办法
解决我的代码行问题
应该是这样的
对未来也很有意思。该文件已正确保存,但我只能在拔出并插入设备后才能看到它。因此,在与pc的连接方面需要某种重启