zoukankan      html  css  js  c++  java
  • Android Sqlite数据库相关——实现将 Sqlite 数据库复制到SD 卡

    1. 确定 sqlite 数据库所在位置(一般在data/data/com.pagename/databases/ 下,其中 com.pagename为当前项目包名)
    2. 确定 sqlite 数据库名称,拼接到数据库位置后面(例如:data/data/com.pagename/databases/user.db)
    3. 指定 SD 卡路径,用于接收 sqlite 文件(例如: Environment.getExternalStorageDirectory() + File.separator +"user.db")
    4. 实现文件复制方法,执行复制

    定义复制参数

    private void copyDBToSDcrad() {
        String DATABASE_NAME = "user.db";
    
        String oldPath = "data/data/com.pagename/databases/" + DATABASE_NAME;
        String newPath = Environment.getExternalStorageDirectory() + File.separator + DATABASE_NAME;
    
        copyFile(oldPath, newPath);
    }
    

    文件复制方法

    /**
     * 复制单个文件
     *
     * @param oldPath String 原文件路径
     * @param newPath String 复制后路径
     * @return boolean
     */
    public static void copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            File newfile = new File(newPath);
            if (!newfile.exists()) {
                newfile.createNewFile();
            }
            if (oldfile.exists()) { // 文件存在时
                InputStream inStream = new FileInputStream(oldPath); // 读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; // 字节数 文件大小
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
            }
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
    
        }
    
    }
    
  • 相关阅读:
    JasperReport html 导出
    mysql 序列号生成器 (自定义函数)
    [Java][Spring]Spring事务不起作用 问题汇总
    序列 mysql
    订单编号
    Mybatis
    SNMP 配置
    Gradle 1.12用户指南翻译——第三十八章. Eclipse 插件
    cocos2dx2.0 与cocos2dx3.1 创建线程不同方式总结
    Android实战简易教程-第二十八枪(Uri转String型实例)
  • 原文地址:https://www.cnblogs.com/ProMonkey/p/5757326.html
Copyright © 2011-2022 走看看