Android 为什么我会遇到FileNotFoundException?我可以在这个位置看到文件,所以我知道它存在
我有一个长ClickListener的文件列表,它会弹出一个带有删除和重命名选项的上下文菜单。它们启动deleteDialog()或renameDialog()。这些函数调用delete()或rename()。删除有效,但重命名将提供:Android 为什么我会遇到FileNotFoundException?我可以在这个位置看到文件,所以我知道它存在,android,file,alert,filenotfoundexception,Android,File,Alert,Filenotfoundexception,我有一个长ClickListener的文件列表,它会弹出一个带有删除和重命名选项的上下文菜单。它们启动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 我甚
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您是否尝试过完全限定目标的文件名?您当前正试图从currentDirectory.toString()+selectedFileString重命名为“test.html” 您可能想尝试currentDirectory.toString()+“test.html”,否则可能会遇到权限问题。您调用了两次
f.renameTo(newFile)
!正常情况下一次,在if()
条件下第二次。我猜第一次它已经被重命名了,所以当你第二次这样做时,它失败了(要么因为文件不再有相同的名称,要么因为它已经有了新的文件名)
尝试删除第一个.f.重命名()
还要注意,返回false可能有各种原因,不仅仅是找不到文件。当然,您会得到FileNotFoundException,因为您自己在代码中抛出它:-)您在清单文件中添加了这一行吗<代码>
f.renameTo(newFile);
if (!f.renameTo(newFile)) {...}