Android-拷贝assets目录文件

 private void CopyAssets() {
  AssetManager assetManager = getAssets();
  String[] files = null;
  try {
   files = assetManager.list("BOOK");
  } catch (IOException e) {
   // Log.e("tag", e.getMessage());
  }

  for (String filename : files) {
   // Log.e("tag", "File name => " + filename);
   InputStream in = null;
   OutputStream out = null;
   try {

    in = assetManager.open("BOOK/" + filename);
    out = new FileOutputStream(Constants.BASE_DIR + filename);
    copyFile(in, out);

   } catch (Exception e) {
    // Log.e("tag", e.getMessage());
   } finally {
    try {
     if (in != null) {
      in.close();
      in = null;
     }
     if (out != null) {
      out.flush();
      out.close();
      out = null;
     }
    } catch (IOException e) {

    }
   }
  }
 }

 private void copyFile(InputStream in, OutputStream out) throws IOException {
  byte[] buffer = new byte[1024];
  int read;
  while ((read = in.read(buffer)) != -1) {
   out.write(buffer, 0, read);
  }
 }

猜你喜欢

转载自blog.csdn.net/qq995475158/article/details/45013571
今日推荐