6.拷贝数据库--SQLiteDatabase.openDatabase()

package org.heima.mobilesafe01.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author U gzip 压缩的工具类
 */
public class GzipUtils {

	

	/**
	 * @param srcFile
	 * @param targetFile
	 *            压缩文件
	 */
	public static void zip(File srcFile, File targetFile) {// srcFile:没有压缩过,targetFile:压缩过
		// GZIPOutputStream
		FileInputStream is;
		try {
			is = new FileInputStream(srcFile);
			FileOutputStream os = new FileOutputStream(targetFile);
			zip(is, os);// 压缩流
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param is
	 * @param os
	 *            压缩流
	 */
	public static void zip(InputStream is, OutputStream os) {
		 GZIPOutputStream gout = null;
		try {
			gout = new GZIPOutputStream(os);
			// 一边读一遍写
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				gout.write(buffer, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ClosableUtils.close(is);
			ClosableUtils.close(gout);
		}
	}

	/**
	 * @param srcFile
	 * @param targetFile
	 *            解压文件
	 */
	public static void unZip(File srcFile, File targetFile) {// srcFile:压缩过,targetFile:没有压缩
		FileInputStream is = null;
		GZIPInputStream gis;
		FileOutputStream os;
		try {
			is = new FileInputStream(srcFile);
			os = new FileOutputStream(targetFile);
			unZip(is, os);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 

	}

	/**
	 * @param gis
	 * @param os
	 *            解压流
	 */
	public static void unZip(InputStream is, OutputStream os) {
		GZIPInputStream gis=null;
		int len = 0;
		byte[] buffer = new byte[1024];
		try {
			gis = new GZIPInputStream(is);
			while ((len = gis.read(buffer)) != -1) {
				os.write(buffer, 0, len);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ClosableUtils.close(gis);
			ClosableUtils.close(os);
		}
	}
}

private void copyAddressDB() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				AssetManager assetManager = getAssets();
				String path = getFilesDir().getAbsolutePath();
				File file = new File(path, "address.db");
				if(file.exists()){// 假如已经存在,就没有必要再copy
					return;
				}					
				try {// alt+方向键< ctrl+o:ctrl+shit+L
					InputStream is = assetManager.open("address.zip");
					FileOutputStream out = new FileOutputStream(file);
					GzipUtils.unZip(is, out);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

3 获取数据库SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);


package org.heima.mobilesafe01.db;

import java.util.regex.Pattern;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author U 号码归属地查询
 */
public class AddressDao {

	/**
	 * @param context
	 * @param phone
	 * @return
	 */
	public static String getCardType(Context context, String phone) {// I ---- O
		// ctrl+alt+j
		String path = context.getFilesDir().getAbsolutePath() + "/address.db";
		SQLiteDatabase database = SQLiteDatabase.openDatabase(path, null,
				SQLiteDatabase.OPEN_READONLY);
		String cardType = "";
		// 正则表达式
		if (phone.matches("^\\d{11}$")) {
			phone = phone.substring(0, 7);
			Cursor cursor = database.rawQuery(
					"select cardtype from info where mobileprefix=?",
					new String[] { phone });
			if (cursor != null) {
				if (cursor.moveToNext()) {
					cardType = cursor.getString(0);
					return cardType;
				}
				cursor.close();
			}
		} else {
			int length = phone.length();
			switch (length) {
			case 3:
				cardType = "紧急号码";
				break;
			case 4:
				cardType = "模拟器";
				break;
			case 5:
				cardType = "服务号码";
				break;
			default:
				cardType = "未知号码";
				break;
			}
		}
		database.close();
		return cardType;
	}
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值