0

我有一个带有 longclicklistener 的文件列表,它会显示一个带有删除和重命名选项的上下文菜单。它们启动 deleteDialog() 或 renameDialog()。它们调用 delete() 或 rename()。删除有效,但重命名给出:

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

甚至认为我可以在这个位置的文件系统上看到这个文件。

这是我的警报代码:

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

}

如您所见,deleteDialog() 和 renameDialog() 的代码相同,但 renameDialog() 抛出 FileNotFoundException

4

2 回答 2

1

您是否尝试过完全限定目标的文件名?您当前正尝试从 currentDirectory.toString()+selectedFileString 重命名为“test.html”。

您可能想尝试 currentDirectory.toString()+"test.html" 否则您可能会遇到权限问题。

于 2013-05-05T13:46:22.370 回答
0

你叫f.renameTo(newFile) 两声!一次正常,第二次在if()条件下。我猜它第一次已经被重命名了,所以当你第二次重命名时,它会失败(因为文件不再具有相同的名称,或者因为它已经具有新的文件名)。

f.renameTo(newFile);

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

尝试删除第一个.f.renameTo().

另请注意,renameTo()返回 false 可能有各种原因,而不仅仅是找不到文件。当然你会得到 FileNotFoundException 因为你自己把它扔到你的代码中:-)

于 2013-05-05T13:55:17.153 回答