欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取assets文件夹中的数据并写入SD卡示例

程序员文章站 2023-02-02 12:09:05
本文示例主要实现了android获取assets文件夹中的数据并将其写入到sd卡中,该程序实现的步骤主要为:首先读取assets文件夹中的数据库,再将其写入到sd存储卡中。...

本文示例主要实现了android获取assets文件夹中的数据并将其写入到sd卡中,该程序实现的步骤主要为:首先读取assets文件夹中的数据库,再将其写入到sd存储卡中。

完整示例代码如下:

import java.io.file;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import android.content.context;
/*将assets文件夹下的数据库写入sd卡中
 * @author dave */
public class writetosd {
 private context context;
 string filepath = android.os.environment.getexternalstoragedirectory()+"/weather";
 public writetosd(context context){
 this.context = context;
 if(!isexist()){
  write();
 }
 }
 private void write(){
 inputstream inputstream;
 try {
  inputstream = context.getresources().getassets().open("addressid.db");
  file file = new file(filepath);
  if(!file.exists()){
  file.mkdirs();
  }
  fileoutputstream fileoutputstream = new fileoutputstream(filepath + "/database.db");
  byte[] buffer = new byte[512];
  int count = 0;
  while((count = inputstream.read(buffer)) > 0){
  fileoutputstream.write(buffer, 0 ,count);
  }
  fileoutputstream.flush();
  fileoutputstream.close();
  inputstream.close();
  system.out.println("success");
 } catch (ioexception e) {
  e.printstacktrace();
 }
 }
 private boolean isexist(){
 file file = new file(filepath + "/database.db");
 if(file.exists()){
  return true;
 }else{
  return false;
 }
 }
}