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();
    
        }
    
    }
    
  • 相关阅读:
    java泛型
    跨域传递
    laravel的一些语法
    去重
    laravel的一些查询语句
    mysql把之前表单进行拆分
    Laravel5.1接收json数据
    thinkphp5 composer安装验证码
    关于地图经纬度的问题
    tp5分组查询
  • 原文地址:https://www.cnblogs.com/ProMonkey/p/5757326.html
Copyright © 2011-2022 走看看