将Android程序中的数据库导出到SD卡

来源:互联网 发布:windows网络编程第三版 编辑:程序博客网 时间:2024/05/22 10:24

有时为了查看表结构,需要将Android程序中的数据库导出到SD卡来:

String DATABASE_NAME = "数据库文件名称";String oldPath = "data/data/应用包名/databases/" + DATABASE_NAME;String newPath = Environment.getExternalStorageDirectory() + File.separator + DATABASE_NAME;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();    }}
0 0
原创粉丝点击