由网友(等不到的离人)分享简介:我想我现有的数据库从我的内部到外部存储复制,但我有一个小问题。它说,的文件不存在。下面是我使用的究竟是什么:从内部文件复制到外部存储:私人无效的CopyFile(SRC字符串,字符串DST)抛出IOException异常{FileChannel随路=新的FileInputStream(SRC).getChannel()...
我想我现有的数据库从我的内部到外部存储复制,但我有一个小问题。它说,的文件不存在
。下面是我使用的究竟是什么:
从内部文件复制到外部存储:
私人无效的CopyFile(SRC字符串,字符串DST)抛出IOException异常{
FileChannel随路=新的FileInputStream(SRC).getChannel();
FileChannel outChannel =新的FileOutputStream(DST).getChannel();
尝试
{
inChannel.transferTo(0,inChannel.size(),outChannel);
}
最后
{
如果(随路!= NULL)
inChannel.close();
如果(outChannel!= NULL)
outChannel.close();
}
}
和我使用的是这样的:
copyFile("/data/data/com.example.test/databases/database.sqlite","/sdcard/.Example/Data/database.sqlite");
,但它不工作,我pretty的肯定,在内部存储的数据库是存在的。
如果我设定为的CopyFile
目标文件夹/ SD卡/。例/数据/
它的建立文件数据
在。例
。
我缺少的是什么有什么建议?
解决方案使用这个数据库复制到SD卡。
公共无效的CopyFile()
{
尝试
{
文件SD = Environment.getExternalStorageDirectory();
文件数据= Environment.getDataDirectory();
如果(sd.canWrite())
{
字符串currentDBPath = 数据 your.package.name数据库 dabase_name;
字符串backupDBPath =数据库名称;
文件currentDB =新的文件(数据,currentDBPath);
文件的某个backupdb =新的文件(SD,backupDBPath);
如果(currentDB.exists()){
FileChannel SRC =新的FileInputStream(currentDB).getChannel();
FileChannel DST =新的FileOutputStream(某个backupdb).getChannel();
dst.transferFrom(源,0,src.size());
src.close();
dst.close();
}
如果(布尔==真)
{
Toast.makeText(Settings.this,备份完成,Toast.LENGTH_SHORT).show();
布尔=假;
}
}
}
赶上(例外五){
Log.w(设置备份,E);
}
}
I'm trying to copy my existing databases from my Internal to External Storage,but I have a little problem. It says that the file does not exist
. Here is what I'm using actually :
to copy files from internal to external storage :
private void copyFile(String src, String dst) throws IOException{
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try
{
inChannel.transferTo(0, inChannel.size(), outChannel);
}
finally
{
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
and I'm using it like this :
copyFile("/data/data/com.example.test/databases/database.sqlite","/sdcard/.Example/Data/database.sqlite");
but it's not working and I'm pretty sure that the database in internal storage is there.
If I set as a destination folder in copyFile
"/sdcard/.Example/Data/"
it's creating file Data
in .Example
.
Any suggestions what I'm missing?
解决方案Use this to copy your database to sdcard.
public void copyFile()
{
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
String currentDBPath = "datayour.package.namedatabasesdabase_name";
String backupDBPath = "database_name";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, 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();
}
if(bool == true)
{
Toast.makeText(Settings.this, "Backup Complete", Toast.LENGTH_SHORT).show();
bool = false;
}
}
}
catch (Exception e) {
Log.w("Settings Backup", e);
}
}
相关推荐
最新文章