title: How to check if file name already exists?
body: <p>I have a voice recording app and I'm trying to implement a feature that checks if the recorded file with a certain name already exists. If a user types in a file name that already exists, an alert dialog should be shown.</p>
  
  <p>All file names are stored in a .txt file on the device.</p>
  
  <p>My current code:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>This code only works halfway as it should. It does successfully check if the file name already exists. If the file name doesn't yet exist, it will work fine. But if the file name already exists, then the user will get the "nameAlreadyExists()" alert dialog but the file will still be added and overwritten. How do I make my code stop at "nameAlreadyExists()"?</p>
  
  <p><strong>I solved the problem with the following code:</strong></p>
  
  <pre><code>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();
              }
  </code></pre>