2013-07-31 31 views
0

保存文件我有以下代碼:Android中

btnSaveTrip.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      if (showLog != null && showLog.getText().toString().length() > 0) { 
       File folder = new File(Environment.getExternalStorageDirectory() + "/tc"); 
       if (!folder.exists()) { 
        folder.mkdir(); 
       } 
       String externalStoragePath = Environment.getExternalStorageDirectory().toString(); 
       final File file = new File(externalStoragePath + "/tc/strip.tcl"); 
       try { 
        if (file.exists()) { 
         new AlertDialog.Builder(getActivity()) 
         .setTitle("File Already Exist") 
         .setMessage("Do you want to overwrite the file?") 
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           outputStream = new FileOutputStream(file); 
           outputStream.write(showLog.getText().toString().getBytes()); 
           Toast.makeText (getActivity(), file.toString(), Toast.LENGTH_SHORT).show(); 
          } 
         }) 
         .setNegativeButton("No", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           // do nothing 
          } 
         }) 
         .show(); 
        } 
        else { 
         outputStream = new FileOutputStream(file); 
         outputStream.write(showLog.getText().toString().getBytes()); 
         Toast.makeText (getActivity(), file.toString(), Toast.LENGTH_SHORT).show(); 
        } 
       } 
       catch (IOException e) { 
        e.printStackTrace(); 
        Toast.makeText (getActivity(), "error in try", Toast.LENGTH_SHORT).show(); 
       } 
       finally { 
        if(outputStream!=null) { 
         try { 
          outputStream.close(); 
          Toast.makeText (getActivity(), "file closed", Toast.LENGTH_SHORT).show(); 
         } 
         catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
          Toast.makeText (getActivity(), "error in finally catch", Toast.LENGTH_SHORT).show(); 
         } 
        } 
       } 
      } 
      else { 
       Toast.makeText (getActivity(), "empty", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

我所希望做的是:

當按鈕被點擊:

1)檢查,使數據不爲空或空(工作細):

if (showLog != null && showLog.getText().toString().length() > 0) { 

2)澈CK,以確保該文件夾存在,如果無法創建文件夾(做工精細):

  File folder = new File(Environment.getExternalStorageDirectory() + "/tc"); 
      if (!folder.exists()) { 
       folder.mkdir(); 
      } 

3)之前將數據寫入到文件,確保它不存在。如果確實存在,請提示用戶查看是否可以覆蓋。如果用戶選擇YES,則覆蓋該文件,但如果用戶選擇NO,則在文件名末尾添加「1」並保存。 (沒有工作,需要幫​​助

我得到一個錯誤的下面一行:

outputStream = new FileOutputStream(file); 
outputStream.write(showLog.getText().toString().getBytes()); 

錯誤:

Unhandled exception type FileNotFoundException

--> (Surround with try/catch)

回答

1

在回答第3部分)時,我編輯了您的代碼以便爲您正常工作。你所擁有的大部分代碼都是正確寫入的,只有幾個問題。我也感動的代碼寫一個新的文件到一個新的方法writeFile()爲了避免複製代碼,並使其更容易跟蹤這是怎麼回事:

btnSaveTrip.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (showLog != null && showLog.getText().toString().length() > 0) { 
      File folder = new File(Environment.getExternalStorageDirectory() + "/TollCulator"); 
      if (!folder.exists()) { 
       folder.mkdir(); 
      } 
      String externalStoragePath = Environment.getExternalStorageDirectory().toString(); 
      final File file = new File(externalStoragePath + "/TollCulator/strip.tcl"); 
      if (file.exists()) { 
       new AlertDialog.Builder(getActivity()) 
       .setTitle("File Already Exist") 
       .setMessage("Do you want to overwrite the existing file?") 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         writeFile(file); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }) 
       .show(); 
      } else { 
       writeFile(file); 
      } 
     } else { 
      Toast.makeText (getActivity(), "empty", Toast.LENGTH_SHORT).show(); 
     } 
    } 
}); 

// ... 

private void writeFile(File file){ 
    try { 
     outputStream = new FileOutputStream(file); 
     outputStream.write(showLog.getText().toString().getBytes()); 
     Toast.makeText (getActivity(), file.toString(), Toast.LENGTH_SHORT).show(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if(outputStream!=null) { 
      try { 
       outputStream.close(); 
       Toast.makeText (getActivity(), "file closed", Toast.LENGTH_SHORT).show(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       Toast.makeText (getActivity(), "error in finally catch", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
} 
+0

很好用。謝謝。 – Si8

3

1)

File tcDir = new File(Environment.getExternalStorageDirectory(),"tc"); 
tcDir.mkdirs(); 
File file = new File(tcdir, "strip.tcl"); 

第一行在外部存儲器directoy中創建一個名爲tc的文件對象。第二行創建磁盤上的任何失蹤的父母。第三行在該目錄內創建一個文件對象

2)你似乎正在這麼做 - 你創建一個文件輸出流,並像你這樣寫。

3)在寫入文件之前,先調用file.exists()。如果存在,你需要彈出一個AlertDialog。如果他們點擊對話框的「是」按鈕,則會寫入一個文件。如果他們選擇不按鈕,你什麼也不做。最好將所有編寫的代碼放入一個單獨的函數中,以便可以在對話框點擊代碼和這裏的!exists代碼中調用它。

+0

寫入文件前,而不是給它預定義名稱,我可以提示用戶命名文件嗎? – Si8

+0

當然你可以,它只是更多的細節。彈出一個對話框,其中包含編輯文本的文件名,並用輸入的字符串替換strip.tcl。 –