【问题标题】:Android; Check if file exists without creating a new one安卓;检查文件是否存在而不创建新文件
【发布时间】:2013-04-20 17:12:33
【问题描述】:

我想检查我的包文件夹中是否存在文件,但我不想创建一个新文件。

File file = new File(filePath);
if(file.exists()) 
     return true;

此代码是否在不创建新文件的情况下进行检查?

【问题讨论】:

  • Test if file exists 的可能重复项
  • @Kunok 我正在检查您的编辑评论:删除了诸如 tanks 之类的词...:P
  • @KevinGuan 哦,是的,我的错,刚从新年前夜派对回家,所以我无法正常写作:)

标签: android file file-io


【解决方案1】:
if(new File("/sdcard/your_filename.txt").exists())){
              // Your code goes here...
}

【讨论】:

    【解决方案2】:

    Kotlin 扩展属性

    创建 File 对象时不会创建文件,它只是一个接口。

    为了更轻松地处理文件,Uri 上有一个现有的.toFile 函数

    您还可以在 File 和/或 Uri 上添加扩展属性,以进一步简化使用。

    val File?.exists get() = this?.exists() ?: false
    val Uri?.exists get() = File(this.toString).exists()
    

    然后只需使用uri.existsfile.exists 进行检查。

    【讨论】:

      【解决方案3】:
      public boolean FileExists(String fname) {
              File file = getBaseContext().getFileStreamPath(fname);
              return file.exists();
      }
      

      【讨论】:

        【解决方案4】:

        它对我有用:

        File file = new File(getApplicationContext().getFilesDir(),"whatever.txt");
            if(file.exists()){
               //Do something
            }
            else{
               //Nothing
             }
        

        【讨论】:

        • 如果您只有文件名而不是其路径,这就是解决方案
        • @Zach 不是真的,它的路径将是我发送的第一个参数( getApplicationContext().getFilesDir() )
        【解决方案5】:

        你的代码块不会创建一个新的,它只会检查它是否已经存在而不是别的。

        File file = new File(filePath);
        if(file.exists())      
        //Do something
        else
        // Do something else.
        

        【讨论】:

        • 不知道为什么在我的情况下这段代码正在创建一个新文件。
        • 如何同时签入子文件夹?
        • 这样是因为没有静态方法: File.exists(String file) ,所以你必须实例化一个新的 File 对象才能访问 'Exists' 方法。
        • 我认为 OP 不希望创建新的文件对象。
        • @AndroDev 否 - 他不想创建新文件,答案会创建新的文件引用。
        【解决方案6】:

        Path 类中的methods 是句法,这意味着它们对 Path 实例进行操作。但最终您必须访问file 系统以验证特定路径是否存在

         File file = new File("FileName");
         if(file.exists()){
         System.out.println("file is already there");
         }else{
         System.out.println("Not find file ");
         }
        

        【讨论】:

          【解决方案7】:

          当您使用此代码时,您并没有创建一个新文件,它只是为该文件创建一个对象引用并测试它是否存在。

          File file = new File(filePath);
          if(file.exists()) 
              //do something
          

          【讨论】:

            【解决方案8】:

            当您说“在您的包文件夹中”时,您是指您的本地应用程序文件吗?如果是这样,您可以使用Context.fileList() 方法获取它们的列表。只需遍历并查找您的文件。假设您使用 Context.openFileOutput() 保存了原始文件。

            示例代码(在 Activity 中):

            public void onCreate(...) {
                super.onCreate(...);
                String[] files = fileList();
                for (String file : files) {
                    if (file.equals(myFileName)) {
                        //file exits
                    }
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-07-22
              • 1970-01-01
              • 2014-05-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-12-01
              • 2013-08-08
              相关资源
              最近更新 更多