Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 此代码是否将现有数据库文件替换为新文件?_Java_Android - Fatal编程技术网

Java 此代码是否将现有数据库文件替换为新文件?

Java 此代码是否将现有数据库文件替换为新文件?,java,android,Java,Android,我正在用这种方法将我的db文件复制到sd卡上。请告诉我sd卡上的文件是否已经存在,然后它是否将替换或不复制 public boolean copyDbToSDCard() { boolean success = false; String SDCardPath = Environment.getExternalStorageDirectory() .getAbsolutePath(); final String DB

我正在用这种方法将我的db文件复制到sd卡上。请告诉我sd卡上的文件是否已经存在,然后它是否将替换或不复制

public boolean copyDbToSDCard() {
        boolean success = false;
        String SDCardPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();
        final String DBPATH = SDCardPath + "/BD/";
        final String DBNAME = "Mydb3.db";
        this.getReadableDatabase();
        File directory = new File(DBPATH);
        if (!directory.exists())
            directory.mkdir();
        close();

        try {
            InputStream mInput = new FileInputStream(DB_PATH + DB_NAME);
            OutputStream mOutput = new FileOutputStream(DBPATH + DBNAME);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = mInput.read(buffer)) > 0) {
                mOutput.write(buffer, 0, length);
            }
            mOutput.flush();
            mOutput.close();
            mInput.close();
            success = true;
        } catch (Exception e) {
            Toast.makeText(myContext,
                    "copyDbToSDCard Error : " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
            e.fillInStackTrace();
        }
        return success;
    }

您可以检查数据库是否可用

像这样

checkDB()
{
try{
      SQLiteDatabase   dbe = SQLiteDatabase.openDatabase("selectedFilePath", null,0);
        Log.d("opendb","EXIST");
        dbe.close();
         // DB exits then delete
        File file = new File(selectedFilePath);
        boolean deleted = file.delete(); <--- this will help you to delete DB

    }
    catch(Exception e)
    {
         // DB not exits code to copy database
    }
 }
checkDB()
{
试一试{
SQLiteDatabase dbe=SQLiteDatabase.openDatabase(“selectedFilePath”,null,0);
Log.d(“opendb”、“EXIST”);
dbe.close();
//数据库退出,然后删除
文件文件=新文件(selectedFilePath);
布尔删除=file.delete();代码:

OutputStream mOutput = new FileOutputStream(DBPATH + DBNAME);


如果文件存在,它将用新数据替换文件内容

测试代码而不是询问代码是否有效如何?它几乎快得多,不会浪费我们的时间,我的目标是sdcard而不是数据包dbName我刚刚测试了类似的代码,如果您愿意,该文件将被新内容覆盖通过FileOutputStream追加一个文件,您必须在构造函数中显式地告诉它:newFileOutputStream(file,true);但是如果两个文件中都没有匹配的数据呢
mOutput.write(buffer, 0, length);