把程序文件下的数据库文件复制到其他目录,需要用到文件复制。

/**
 * @author ptz
 * @date 2022/02/05
 * @description
 */
public class CopyFile {
    private static final String TAG = "CopyFile";
    /**
     * 根据文件路径拷贝文件
     * @param srcPath 源文件
     * @param destDir 目标文件路径
     * @param fileName 目标文件名
     * @return boolean 成功true、失败false
     */
    public static boolean copyFile(String srcPath, String destDir, String fileName) {
        boolean result = false;

        DLog.w(TAG, "copyFile: src: " + srcPath + ", dest: " + destDir + fileName);

        File src = new File(srcPath);
        if ((src == null) || (destDir== null)) {
            return false;
        }

        File dir = new File(destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        File dest= new File(destDir + fileName);
        if (dest!= null && dest.exists()) {
            boolean ret = dest.delete(); // delete file
            DLog.d(TAG, "copyFile: delete dest result: " + ret);
        }

        try {
            boolean ret = dest.createNewFile();
            DLog.d(TAG, "copyFile: create dest result: " + ret);
        } catch (IOException e) {
            //e.printStackTrace();
            DLog.d(TAG, Log.getStackTraceString(e));
        }

        FileChannel srcChannel = null;
        FileChannel dstChannel = null;

        try {
            srcChannel = new FileInputStream(src).getChannel();
            dstChannel = new FileOutputStream(dest).getChannel();
            srcChannel.transferTo(0, srcChannel.size(), dstChannel);
            result = true;
        } catch (FileNotFoundException e) {
            //e.printStackTrace();
            DLog.d(TAG, Log.getStackTraceString(e));
        } catch (IOException e) {
            //e.printStackTrace();
            DLog.d(TAG, Log.getStackTraceString(e));
        }

        try {
            srcChannel.close();
            dstChannel.close();
        } catch (Exception e) {
            //e.printStackTrace();
            DLog.d(TAG, Log.getStackTraceString(e));
        }

        return result;
    }
}
/**
 * @author ptz
 * @date 2022/02/05
 * @description
 */
public class FileName {
    /**
     * 仅保留文件名不保留后缀
     */
    public static String getFileName(String path) {
        int start = path.lastIndexOf("/");
        int end = path.lastIndexOf(".");
        if (start != -1 && end != -1) {
            return path.substring(start + 1, end);
        } else {
            return null;
        }
    }

    /**
     * 保留文件名及后缀
     */
    public static String getFileNameWithSuffix(String path) {
        int start = path.lastIndexOf("/");
        if (start != -1 ) {
            return path.substring(start + 1);
        } else {
            return null;
        }
    }

    /**
     * 仅保留目录名
     */
    public static String getDirName(String path) {
        int start = 0;
        int end = path.lastIndexOf("/");
        if (start != end && end != -1) {
            return path.substring(start, end + 1);
        } else {
            return null;
        }
    }
}

参考

可能是最完美的Android复制拷贝文件的实例(Java NIO速度快)
https://blog.csdn.net/mvpstevenlin/article/details/54090722
java如何操作字符串取得绝对路径中的文件名及文件夹名
https://blog.csdn.net/weiqiang_1989/article/details/50987852

标签: application

添加新评论