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

来源:互联网 发布:整站优化seo 上海 编辑:程序博客网 时间:2024/05/01 10:46
private void copyDBToSDcrad(){String DATABASE_NAME = "数据库文件名称";String oldPath = "data/data/com.packagename/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();}}

0 0