主要包括copy目录、读sdcard上的配置文件(比读delay多少秒copy)、调用copy(将应用目录下的files及databases复制)

 

/**
     * 复制文件夹及其中的文件
     *
     * @param oldPath String 原文件夹路径 如:data/user/0/com.test/files
     * @param newPath String 复制后的路径 如:data/user/0/com.test/cache
     * @return <code>true</code> if and only if the directory and files were copied;
     * <code>false</code> otherwise
     */
    public static boolean copyFolder(String oldPath, String newPath) {
        Logger.d(TAG, "copyFolder in, oldPath: " + oldPath + ", newPath: " + newPath);
        try {
            File newFile = new File(newPath);
            if (!newFile.exists()) {
                if (!newFile.mkdirs()) {
                    Logger.e(TAG, "copyFolder: cannot create directory.");
                    return false;
                }
            }
            File oldFile = new File(oldPath);
            String[] files = oldFile.list();
            File temp;
            for (String file : files) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file);
                } else {
                    temp = new File(oldPath + File.separator + file);
                }

                if (temp.isDirectory()) {   //如果是子文件夹
                    copyFolder(oldPath + "/" + file, newPath + "/" + file);
                } else if (!temp.exists()) {
                    Logger.e(TAG, "copyFolder:  oldFile not exist.");
                    return false;
                } else if (!temp.isFile()) {
                    Logger.e(TAG, "copyFolder:  oldFile not file.");
                    return false;
                } else if (!temp.canRead()) {
                    Logger.e(TAG, "copyFolder:  oldFile cannot read.");
                    return false;
                } else {
                    FileInputStream fileInputStream = new FileInputStream(temp);
                    FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
                    byte[] buffer = new byte[1024];
                    int byteRead;
                    while ((byteRead = fileInputStream.read(buffer)) != -1) {
                        fileOutputStream.write(buffer, 0, byteRead);
                    }
                    fileInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取copy应用内部数据的延迟时间
     * @return -1: 不执行,0:立即,>0:具体delay时长
     */
    public static int getCopyAppDataDelayTime() {
        int result = 0;
        try {
            File configFile = new File(getSDCardPath() + "/" + DEV_CONFIG_FILE);
            byte[] configContent = getBytesFromFile(configFile);
            JSONObject jsonObject = JSONObject.parseObject(new String(configContent));
            result = jsonObject.getInteger("copyDataDelayTime");
        } catch (Exception e) {
            Logger.e(TAG, e.getMessage());
        }
        return result;
    }

    /**
     * 将应用目录下数据copy至sdcard根目录
     */
    public static void copyAppDataToSdCard(final Context context, final int time) {
        if (isSupportRnDebug()) {
            ThreadPoolExecutor.executeOnBackgroundThread(new Runnable() {
                @Override
                public void run() {
                    if (time >= 0) {
                        try {
                            Thread.sleep(time * 1000);
                            String basePath = getSDCardPath() + "/JJData";
                            copyFolder(context.getFilesDir().getPath().toString(), basePath + "/files");
                            copyFolder(context.getFilesDir().getPath().toString() + "/../databases/", basePath + "/databases");
                        } catch (InterruptedException e) {
                            Logger.e(TAG, "copyAppDataToSdCard in, " + e.getMessage());
                        }
                    }
                }
            });
        }
    }

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐