zoukankan      html  css  js  c++  java
  • 读取Assets所存在的所有文件(遍历每一个文件夹),并存入sdcard里面

    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();
                }
            }

        }
  • 相关阅读:
    混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
    SQL中获取自增长的最大ID
    (inline)内联函数在IOS开发中的使用
    MS SQL SERVER 2005 高可用性之日志传送
    19_toast通知和notify通知 onTouch事件响应
    20 按比例设置 子控件的宽度和高度
    18_SurfaceView 其他线程绘图
    使用Microsoft Media Service实现网络影音多媒体应用系列第三篇技术要点
    使用Microsoft Media Service实现网络影音多媒体应用系列第二篇开发须知
    MVC3WIN7下的IIS7.5部署MVC3应用程序
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2764872.html
Copyright © 2011-2022 走看看