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

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

		}

	}

posted @ 2017-04-20 14:03  wzzkaifa  阅读(306)  评论(0编辑  收藏  举报