android将asseet当中的数据库文件拷到程序目录

/**
         * 将asseet当中的数据库文件拷到程序目录
         * @param activity        当前Activity
         * @param filePath        你的程序目录        getApplicationContext().getFilesDir().getAbsolutePath();
         * @param fileName        你的数据库名称
         */
        public static void copyEmbassy2Databases(Activity activity, String filePath, String fileName) {
                System.out.println("-->copyEmbassy2Databases");
                File oldfile = new File(filePath, fileName);

                if (oldfile.exists())
                        oldfile.delete();

                File file = new File(filePath, fileName);

                if (file.exists())
                        return;

                file.getParentFile().mkdirs();

                InputStream in = null;
                OutputStream out = null;

                try {

                        out = new FileOutputStream(file);
                        byte[] buff = new byte[1024];
                        int len = 0;
                        in = activity.getAssets().open(fileName);
                        while ((len = in.read(buff)) > 0) {
                                out.write(buff, 0, len);
                        }
                        out.flush();
                        in.close();

                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if (out != null)
                                        out.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }

                }
        }

 

posted @ 2015-04-17 18:11  小菜希  阅读(336)  评论(0编辑  收藏  举报