【问题标题】:How to check if file name already exists?如何检查文件名是否已经存在?
【发布时间】:2013-08-29 13:06:39
【问题描述】:

我有一个录音应用程序,我正在尝试实现一个功能来检查具有特定名称的录制文件是否已经存在。如果用户键入一个已经存在的文件名,应该会显示一个警告对话框。

所有文件名都存储在设备上的 .txt 文件中。

我当前的代码:

try {
    BufferedReader br = new BufferedReader(new FileReader(txtFilePath));
    String line = null;

    while ((line = br.readLine()) != null) {
        if (line.equals(input.getText().toString())) {
            nameAlreadyExists();
        }
    }
    br.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException ex) {
    ex.printStackTrace();
}

newFileName = input.getText();

from = new File(appDirectory, beforeRename);
to = new File(appDirectory, newFileName + ".mp3");
from.renameTo(to);
writeToFile(input);
toast.show();

此代码仅能正常工作。它确实成功地检查了文件名是否已经存在。如果文件名尚不存在,它将正常工作。但是如果文件名已经存在,那么用户将得到“nameAlreadyExists()”警告对话框,但文件仍然会被添加和覆盖。如何让我的代码停在“nameAlreadyExists()”处?

我用下面的代码解决了这个问题:

File newFile = new File(appDirectory, input.getText().toString() + ".mp3");
if (newFile.exists())
            {
                nameAlreadyExists();
            }
            else
            {
                newFileName = input.getText();

                from = new File (appDirectory, beforeRename);
                to = new File (appDirectory, newFileName + ".mp3");
                from.renameTo(to);
                writeToFile(input);
                toast.show();
            }

【问题讨论】:

    标签: java android


    【解决方案1】:

    File 类提供了exists() 方法,如果文件存在则返回true

    File f = new File(newFileName);
    if(f.exists()) { /* show alert */ }
    

    【讨论】:

    • 非常感谢!有很多答案,但你的答案是最简单和最有效的,它就像一个魅力:)
    • 如何同时签入子文件夹?
    【解决方案2】:

    你可以很容易地写return; 来退出函数(如果那是函数)。或者使用

    if(f.exists() /* f is a File object */ ) /* That is a bool, returns true if file exists */
    

    语句,检查文件是否存在,然后做正确的事情。

    【讨论】:

      【解决方案3】:

      下面是我用来完成任务的代码,

      File mediaStorageDir = new File(
                          Environment
                                  .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                          "My_Folder");
      
                  if (!mediaStorageDir.exists()) {
                      if (!mediaStorageDir.mkdirs()) {
                          Log.d("My_Folder", "failed to create directory");
                          return null;
                      }
                  }
      

      【讨论】:

        【解决方案4】:

        如果文件确实存在,我认为您缺少一些分叉代码的标志:

            boolean  fileExists = false;
            try {
        
            BufferedReader br = new BufferedReader(new FileReader(txtFilePath));
            String line = null;
        
            while ((line = br.readLine()) != null) {
                if (line.equals(input.getText().toString())) {
                    fileExists = true;
                    nameAlreadyExists();
                }
            }
            br.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        if(!fileExists)
        {
         newFileName = input.getText();
        
         from = new File(appDirectory, beforeRename);
         to = new File(appDirectory, newFileName + ".mp3");
         from.renameTo(to);
         writeToFile(input);
         toast.show();
        }
        

        并随意使用上述 File 的 exists() 函数....

        【讨论】:

          猜你喜欢
          • 2021-11-28
          • 2017-04-27
          • 1970-01-01
          • 1970-01-01
          • 2015-06-17
          • 2016-08-08
          • 2013-10-28
          • 2022-08-19
          • 1970-01-01
          相关资源
          最近更新 更多