【代码】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();
  }
相关推荐
zh_xuan20 小时前
Android 待办事项增加事项统计
android
zopple20 小时前
Laravel 10.x新特性全解析
android
鬼先生_sir20 小时前
MySQL进阶-SQL高级语法全解析
android
Kapaseker20 小时前
lazy 与 lateinit 到底有什么区别?
android·kotlin
黄林晴20 小时前
慌了!Android 17 取消图标文字,你的 App 可能要找不到了
android
空中海20 小时前
3.4 状态同步与生命周期管理
android·网络
砖厂小工21 小时前
Android 开发的 AI coding 与 AI debugging
android·ai编程
peakmain921 小时前
CmComposeUI —— 基于 Kotlin Multiplatform Compose 的 UI 组件库
android
studyForMokey21 小时前
【Android面试】Glide专题
android·面试·glide
m0_7381207221 小时前
渗透知识ctfshow——Web应用安全与防护(三)
android·前端·安全