2016-10-06 56 views
0

當按下按鈕Save_btn時,嘗試寫入文件,但是當我運行應用程序時,它運行平穩,沒有錯誤,但文件無處可尋。在Android應用程序中寫入並保存文件

我正在嘗試寫入設備的內部存儲器。正在編寫的文本位於edittext字段中。我想從EditText的這個文本被寫入文件

我已經包括我在下面使用的代碼;

Save_btn = (Button) findViewById(R.id.g); 
    Save_btn.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View views) { 
             TextView CodeView = (TextView) findViewById(R.id.Code_Viewer); 
             CodeView.setText(CodeView.getText()); 
             try { 
              String etName = CodeView.getText().toString(); 
              if (!etName.trim().equals("")) { 
               File file = new File("/Documents/test.txt"); 

               //if file doesnt exists, then create it 
               if (!file.exists()) { 
                file.createNewFile(); 
               } 


               FileWriter fileWritter = new FileWriter(file.getName(), true); 
               BufferedWriter bufferWritter = new BufferedWriter(fileWritter); 
               bufferWritter.write(etName); 
               bufferWritter.close(); 
              } 
             } catch (IOException e) { 

              e.printStackTrace(); 
             } 
            } 
           } 
    ); 

任何關於如何讓文件正確寫入的建議將不勝感激。

在此先感謝。

+0

首先,您想在哪裏保存文件?你想達到什麼目的? https://developer.android.com/guide/topics/data/data-storage.html – user1506104

+0

我正在嘗試寫入設備的內部存儲。正在編寫的文本位於edittext字段中。我想從EditText中將這段文字寫入文件 – bdg

+0

No.'File file = new File(「/ Documents/test.txt」);'。這是一條不可能的道路。把'Toast()'放在顯示'e.getMessage()'的catch塊中。 – greenapps

回答

1

與你替換此代碼:

Save_btn = (Button) findViewById(R.id.g); 
    Save_btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View views) { 
       TextView CodeView = (TextView) findViewById(R.id.Code_Viewer); 
       CodeView.setText(CodeView.getText()); 
       String etName = CodeView.getText().toString(); 

       File dir = new File(getFilesDir(), "yourfolder"); 
       if(!dir.exists()) 
       { 
        dir.mkdirs(); 
       } 
       String textFileName="textFile.txt"; 
       File file = new File(dir.getPath(), textFileName); 
       if(file.exists()) 
       { 
        file.delete(); 
       } 
       try { 
        FileWriter fw = new FileWriter(file); 
        BufferedWriter bw = new BufferedWriter(fw); 
        bw.write(etName); 
        bw.close(); 

       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 


      } 
     } 
    ); 

你應該注意以下幾點:

  • 此代碼將創建/數據/數據/ [您的應用程序包名稱]的文件/文件/ [你的文件夾]/[你的文本文件名]
  • 如果文件名相同,它總是會刪除你的文件,所以你應該得到每個文件名的唯一名稱(你可以在文件名中包含日期和時間)
+0

感謝您的建議,仍然不能看到您使用物理電話或模擬器的應用程序的目錄 – bdg

+0

? –

+0

物理電話華爲P9 – bdg

0

我不確定這可以工作,因爲您指定的文件路徑是a)絕對路徑和b)指向您可能沒有寫入權限的目錄。

File file = new File("/Documents/test.txt"); 

這引用文件系統根目錄中的Documents文件夾而不是應用程序文件。

如果您想本地保存以供您自己的應用程序使用,您可以嘗試Context#getFilesDir,例如,

File file = new File(context.getFilesDirectory(), "yourfile.txt"); 

如果你想保存在某個地方它的其他應用程序可以使用它,你可能需要一個更復雜的方法,例如一個FileProvider

+0

嘗試此操作時,出現錯誤「無法解析符號上下文」? – bdg

+1

沒有上下文。它的含蓄。 – greenapps

+0

你的活動是一個上下文(你的應用程序也是這樣),你的片段有一個方法getContext()。如果在一個活動中發生這種情況,請在單擊處理程序之前嘗試'final Context context = this;'。或者使用greenapps方法。 – stefs

0

您應該使用

File file = new File(context.getFilesDir(), "test.txt");

,而不是

File file = new File("/Documents/test.txt");

保存到內部存儲。其餘的代碼可以保持不變。

相關問題