栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android获取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;

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;
 }
 }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/164550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号