【代码】Android|判断asserts下的文件存在与否,以及普通文件存在与否

asserts

复制代码
  boolean isAssertFileExists(String filename) {
      AssetManager assetManager = getActivity().getAssets();
      try {
          String[] files = assetManager.list("");
          for (String file : files) {
              if (file.equals(filename)) {
                  return true;
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      return false;
  }

需要引入的头文件:

复制代码
import java.io.IOException;
import android.content.res.AssetManager;

可以看到能正常判断:

其他外部文件或者data下的文件

如果是常规文件只需要用new File(filepath).exists()就行了。写成函数如下:

复制代码
  boolean checkFileExists(String dirname, String filename) {
    File file = new File(dirname, filename);
    return file.exists();
  }
相关推荐
hjlgs3 小时前
framework修改快速验证
android
游戏开发爱好者84 小时前
iOS 开发者的安全加固工具,从源码到成品 IPA 的多层防护体系实践
android·安全·ios·小程序·uni-app·cocoa·iphone
安卓理事人4 小时前
安卓内存泄露排查LeakCanary
android
秃了也弱了。5 小时前
MySQL空间函数详解,MySQL记录经纬度并进行计算
android·数据库·mysql
.豆鲨包6 小时前
【Android】Binder机制浅析
android·binder
Nerve8 小时前
GooglePay: API 文档
android·google
Nerve8 小时前
GooglePay: 订阅商品购买流程
android·google
summerkissyou19878 小时前
Audio-触摸音-播放流程
android
Nerve8 小时前
GooglePay: 消耗商品购买流程
android·google
LiteHeaven8 小时前
Android 8.1 Unable to create application data 问题分析
android