RapsFan1981
RapsFan1981

Reputation: 1197

Why am I getting a FileNotFoundException? I can see the file at this location so I know it exists

I have a file list with a longclicklistener that brings up a context menu with delete and rename options. These launch either a deleteDialog() or renameDialog(). These call either delete() or rename(). The delete works but rename is giving:

05-05 10:26:44.105: W/System.err(19017884): java.io.FileNotFoundException: Failed to rename file: /sdcard/My Webs/new/index.php

Even thought I can see this file on the filesystem at this location.

Here is my code for the Alerts:

void delete(File f) throws IOException {
    if (f.isDirectory()) {
        for (File c : f.listFiles())
            delete(c);
    }
    if (!f.delete())
        throw new FileNotFoundException("Failed to delete file: " + f);
}

void rename(File f, String newName) throws IOException {
    File newFile = new File(newName);

    f.renameTo(newFile);

    if (!f.renameTo(newFile))
        throw new FileNotFoundException("Failed to rename file: " + f);
}

public void delDialog(int position) {
    final int pos = position;

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setIcon(R.drawable.remove);
    alertDialog.setTitle(getString(R.string.delete));

    alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            String selectedFileString = directoryEntries.get(pos).getText();
            File tmpFile = new File(currentDirectory.toString()
                    + selectedFileString);

            try {
                delete(tmpFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            directoryEntries.remove(pos);
            itla.notifyDataSetChanged();

            currentFile = null;
            changed = false;
            return;
        }

    });
    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();
        }
    });

    alertDialog.setMessage("Are you sure you want to delete this file?");
    alertDialog.show();
}

public void renameDialog(int position) {
    final int pos = position;

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setIcon(R.drawable.renameicon);
    alertDialog.setTitle(getString(R.string.rename));

    alertDialog.setButton("Rename", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {

            String selectedFileString = directoryEntries.get(pos).getText();
            File tmpFile = new File(currentDirectory.toString()
                    + selectedFileString);

            try {
                rename(tmpFile, "test.html");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            itla.notifyDataSetChanged();

            return;
        }

    });
    alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();
        }
    });

    alertDialog.setMessage("Are you sure you want to rename this file?");
    alertDialog.show();
}

public void Show_Context(Context context, String message, int position) {
    final AlertDialog customDialog = new AlertDialog.Builder(this).create();
    LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.contextmenu, null);
    final Button del = (Button) view.findViewById(R.id.delBtn);
    final Button rename = (Button) view.findViewById(R.id.renameBtn);
    final int pos = position;
    customDialog.setView(del);
    customDialog.setView(rename);

    del.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            customDialog.dismiss();
            delDialog(pos);
        }
    });

    rename.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            customDialog.dismiss();
            renameDialog(pos);
        }
    });

    customDialog.setView(view);
    customDialog.show();

}

As you can see the code for the deleteDialog() and renameDialog() is the same yet the renameDialog() throws the FileNotFoundException

Upvotes: 0

Views: 240

Answers (2)

Ridcully
Ridcully

Reputation: 23655

You call f.renameTo(newFile) two times! One time normally, and a second time within the if() condition. I guess it already gets renamed the first time, so when you do it a second time, it fails (either because the file doesn't have the same name anymore or because it already has the new filename).

f.renameTo(newFile);

if (!f.renameTo(newFile)) {...}

Try and remove the first .f.renameTo().

Also note, that renameTo() returning false can have all kinds of reasons, not just that the file cannot be found. And of course you get the FileNotFoundException because you throw it yourself in your code :-)

Upvotes: 0

zeetoobiker
zeetoobiker

Reputation: 2789

Have you tried fully qualifying the filename of the destination? You're currently attempting to rename to "test.html" from currentDirectory.toString()+selectedFileString.

You probably want to try currentDirectory.toString()+"test.html" as you might be running into permissions issues otherwise.

Upvotes: 1

Related Questions