把assets下的指定文件拷贝到sdcard上
2013-08-06 14:18
302 查看
由于res下的values、xml等资源文件是不可更改的所以可以把文件放到assets下,放到sdcard上,然后在进行修改。
方法如下:
也可以把assets下的文件进行遍历
方法如下:
AssetManager assets = getResources().getAssets();
try { //判断sdcard是否存在 String status = Environment.getExternalStorageState(); if (status.equals(Environment.MEDIA_MOUNTED)) { String xmlPath = File.separator + "sdcard" + File.separator+"appserver.xml"; File outFile = new File(xmlPath); if(!outFile.exists()){ System.out.println("--建立文件---"); //outFile.mkdirs(); //创建不存在的文件,如果没有这句会出现找不到文件的异常 outFile.createNewFile(); //取到assets下的指定文件,变成流 InputStream in = assets.open("appserver.xml"); //向指定新文件里写入 FileOutputStream out = new FileOutputStream(outFile); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) >=0) { out.write(buf, 0, len); } //关闭流 in.close(); out.close(); } } else { System.out.println("sdcard不存在"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
也可以把assets下的文件进行遍历
private void CopyAssets(String assetDir, String dir) { String[] files; try { // 获得Assets一共有几多文件 files = this.getResources().getAssets().list(assetDir); } catch (IOException e1) { return; } File mWorkingPath = new File(dir); // 如果文件路径不存在 if (!mWorkingPath.exists()) { // 创建文件夹 if (!mWorkingPath.mkdirs()) { // 文件夹创建不成功时调用 } } for (int i = 0; i < files.length; i++) { try { // 获得每个文件的名字 String fileName = files[i]; // 根据路径判断是文件夹还是文件 if (!fileName.contains(".")) { if (0 == assetDir.length()) { CopyAssets(fileName, dir + fileName + "/"); } else { CopyAssets(assetDir + "/" + fileName, dir + "/" + fileName + "/"); } continue; } File outFile = new File(mWorkingPath, fileName); if (outFile.exists()) outFile.delete(); InputStream in = null; if (0 != assetDir.length()) in = getAssets().open(assetDir + "/" + fileName); else in = getAssets().open(fileName); OutputStream out = new FileOutputStream(outFile); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
相关文章推荐
- android 将Assets中的文件拷贝到sdcard上
- android 将Assets中的文件拷贝到sdcard上
- 把apk安装到Android模拟器中、把文件拷贝进sdcard
- 向android模拟器SDcard中拷贝文件
- Android File 转Inputstram,跳转手机sdcard 获取指定文件
- Android 读取sdcard指定目录文件
- 读取Assets所存在的所有文件(遍历每一个文件夹),并存入sdcard里面
- Android开发实践:拷贝assets到sdcard
- Android编程读取Assets所有文件(遍历每一个文件夹)并存入sdcard的方法
- 把外部文件拷贝的AVD安卓模拟器上的sdcard上,并且在AVD中浏览sdcard的文件
- 将电脑本地的文件拷贝到android虚拟机的虚拟存储卡(sdcard)
- Android--取出SDcard卡上指定后缀名的文件
- android解压缩assets里面的压缩文件到sdcard里面
- 读取Assets所存在的文件,并存入sdcard里面
- Android--取出SDcard卡上指定后缀名的文件
- 【Android】把外部文件拷贝的AVD安卓模拟器上的sdcard上,并且在AVD中浏览sdcard的文件
- 向android模拟器SDcard中拷贝文件
- Android从Assets拷贝文件以及文件夹到指定目录
- 【Android】把外部文件拷贝的AVD安卓模拟器上的sdcard上,而且在AVD中浏览sdcard的文件
- Android 手机sdcard目录或文件的拷贝、移动、删除(递归)