2013-05-05 78 views
0

我有一個文件列表與一個longclicklistener,帶來刪除和重命名選項的上下文菜單。這些啓動一個deleteDialog()或renameDialog()。這些調用delete()或rename()。刪除工作,但重命名是給:爲什麼我得到FileNotFoundException?我可以看到這個位置的文件,所以我知道它存在

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異常

+0

你有沒有在清單文件中加入這一行? '' – 2013-05-05 13:55:26

回答

1

有無你試圖完全限定目的地的文件名?您目前正試圖從currentDirectory.toString()+ selectedFileString重命名爲「test.html」。

您可能想要嘗試使用currentDirectory.toString()+「test.html」,否則您可能會遇到權限問題。

+0

謝謝!就是這樣:) – RapsFan1981 2013-05-05 13:59:01

0

您可以撥打f.renameTo(newFile)兩次!正常情況下一次,在if()條件下第二次。我想它已經第一次被重命名了,所以當你第二次做時,它會失敗(或者因爲文件不再有相同的名字或者因爲它已經有了新的文件名)。

f.renameTo(newFile); 

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

嘗試並移除第一個.f.renameTo()

另請注意,返回false的renameTo()可能有各種原因,不僅僅是文件找不到。當然,你會得到FileNotFoundException,因爲你自己把它放在你的代碼中:-)

相關問題