码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-09-02 14:16:36      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:android   数据库   

	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();

		}

	}

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

标签:android   数据库   

原文地址:http://blog.csdn.net/wangyuexing_blog/article/details/39004675

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!