Guy
Guy

Reputation: 6522

How to check if file name already exists?

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.

All file names are stored in a .txt file on the device.

My current 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();

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()"?

I solved the problem with the following 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();
            }

Upvotes: 3

Views: 26402

Answers (4)

KocsisLaci
KocsisLaci

Reputation: 59

I think you are missing some flag to fork your code in case the file does exist:

    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();
}

and feel free to use the exists() function of File as above....

Upvotes: 0

Spring Breaker
Spring Breaker

Reputation: 8251

Below is the code i used to do the task,

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;
                }
            }

Upvotes: 0

TN888
TN888

Reputation: 7739

You can easy write return; to get out from the function (if that is the function). Or use

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

statement, to check if file exists and then do correct things.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

The File class provides the exists() method, which returns true if the file exists.

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

Upvotes: 19

Related Questions