2012-05-22 61 views
0

我正在創建一個小應用程序,讓用戶閱讀/ data/local中某個文本文件中的內容,編輯它,然後保存它。 我已經通過在這裏和那裏使用一些教程得到了一切工作,但仍有一些工作不正常。寫入文件並從/ data/local讀取數據?

已經實現了root訪問,並且寫入/讀取文件也完成了,但是當按下「Write」按鈕時,我得到一個表示「打開失敗:EACCES(Permission denied)」的敬酒。不幸的是,Google對此並沒有多大的幫助。 另外,我正在使用WRITE_TO_EXTERNAL_STORAGE權限。

代碼:

package bas.sie.datadatafix; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 

import android.os.Bundle; 
import android.os.Environment; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.actionbarsherlock.app.SherlockActivity; 

public class DataFixActivity extends SherlockActivity { 

    EditText txtData; 
    Button btnReadSDFile; 
    Button btnWriteSDFile; 
    Button btnReadSkipFile; 
    Button btnWriteSkipFile; 
    Button btnClearScreen; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Process p; 
     try { 
      // Preform su to get root privledges 
      p = Runtime.getRuntime().exec("su"); 

      // Attempt to write a file to a root-only 
      DataOutputStream os = new DataOutputStream(p.getOutputStream()); 
      os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n"); 

      // Close the terminal 
      os.writeBytes("exit\n"); 
      os.flush(); 
      try { 
       p.waitFor(); 
        if (p.exitValue() != 255) { 
         // TODO Code to run on success 
         Toast.makeText(this, "root", Toast.LENGTH_LONG); 
        } 
        else { 
         // TODO Code to run on unsuccessful 
         Toast.makeText(this, "No root", Toast.LENGTH_LONG); 
        } 
      } catch (InterruptedException e) { 
       // TODO Code to run in interrupted exception 
       Toast.makeText(this, "No root", Toast.LENGTH_LONG); 
      } 
     } catch (IOException e) { 
      // TODO Code to run in input/output exception 
      Toast.makeText(this, "NO root", Toast.LENGTH_LONG); 
     } 

     if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){ 
      Toast.makeText(this, "External SD card not mounted", Toast.LENGTH_LONG).show(); 
     } 

     txtData = (EditText) findViewById(R.id.txtData); 

     btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile); 
     btnReadSDFile.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // write on SD card file data in the text box 
       try { 
        File myFile = new File("/data/local/move_cache.txt"); 
        FileInputStream fIn = new FileInputStream(myFile); 
        BufferedReader myReader = new BufferedReader(
          new InputStreamReader(fIn)); 
        String aDataRow = ""; 
        String aBuffer = ""; 
        while ((aDataRow = myReader.readLine()) != null) { 
         aBuffer += aDataRow + "\n"; 
        } 
        txtData.setText(aBuffer); 
        myReader.close(); 
        Toast.makeText(getBaseContext(), 
          "Done reading from SD: 'move_cache.txt'", 
          Toast.LENGTH_SHORT).show(); 
       } catch (Exception e) { 
        Toast.makeText(getBaseContext(), e.getMessage(), 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 

     btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile); 
     btnWriteSDFile.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // write on SD card file data in the text box 
       try { 
        File myFile = new File("/data/local/move_cache.txt"); 
        myFile.createNewFile(); 
        FileOutputStream fOut = new FileOutputStream(myFile); 
        OutputStreamWriter myOutWriter = new OutputStreamWriter(
          fOut); 
        myOutWriter.append(txtData.getText()); 
        myOutWriter.close(); 
        fOut.close(); 
        Toast.makeText(getBaseContext(), 
          "Done writing to SD: 'move_cache.txt'", 
          Toast.LENGTH_SHORT).show(); 
       } catch (Exception e) { 
        Toast.makeText(getBaseContext(), e.getMessage(), 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 

     btnReadSkipFile = (Button) findViewById(R.id.btnReadSkipFile); 
     btnReadSkipFile.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // write on SD card file data in the text box 
       try { 
        File myFile = new File("/data/local/skip_apps.txt"); 
        FileInputStream fIn = new FileInputStream(myFile); 
        BufferedReader myReader = new BufferedReader(
          new InputStreamReader(fIn)); 
        String aDataRow = ""; 
        String aBuffer = ""; 
        while ((aDataRow = myReader.readLine()) != null) { 
         aBuffer += aDataRow + "\n"; 
        } 
        txtData.setText(aBuffer); 
        myReader.close(); 
        Toast.makeText(getBaseContext(), 
          "Done reading from SD: 'skip_apps.txt'", 
          Toast.LENGTH_SHORT).show(); 
       } catch (Exception e) { 
        Toast.makeText(getBaseContext(), e.getMessage(), 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 

     btnWriteSkipFile = (Button) findViewById(R.id.btnWriteSkipFile); 
     btnWriteSkipFile.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // write on SD card file data in the text box 
       try { 
        File myFile = new File("/data/local/skip_apps.txt"); 
        myFile.createNewFile(); 
        FileOutputStream fOut = new FileOutputStream(myFile); 
        OutputStreamWriter myOutWriter = new OutputStreamWriter(
          fOut); 
        myOutWriter.append(txtData.getText()); 
        myOutWriter.close(); 
        fOut.close(); 
        Toast.makeText(getBaseContext(), 
          "Done writing to SD: 'skip_apps.txt'", 
          Toast.LENGTH_SHORT).show(); 
       } catch (Exception e) { 
        Toast.makeText(getBaseContext(), e.getMessage(), 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 

     btnClearScreen = (Button) findViewById(R.id.btnClearScreen); 
     btnClearScreen.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // clear text box 
       txtData.setText(""); 
      } 
     }); 


    }// onCreate 

} 

由於提前,

巴斯

回答

1

試試這個邏輯,寫作和閱讀的本地存儲的文件,只餘了,雖然我知道ü沒有它,但仍然..使用WRITE_TO_EXTERNAL_STORAGE權限。

從文件

File f = new File("my.txt"); 
FileReader fr = new FileReader(f); 
BufferedReader br = new BufferedReader(fr); 

String s = null; 

while ((br=readLine())!=null) { 

// Do whatever u want to do with the content of the file,eg print it on console using SysOut...etc 

} 

br.close(); 

閱讀寫入文件:

Boolean isDone = true; 
Scanner scan = new Scanner(System.in); 
File f = new File("my.txt"); 
FileWriter fr = new FileWriter(f); 
BufferedWriter br = new BufferedWriter(fr); 

while (b) { 

    if (!b) { 

br.write(new Scanner(System.in).nextLine()); 

} 


} 
+0

你能指出我在正確的方向,以我應該怎麼做寫的,即內容的一個EditText到/ data/local中的文件?我從來沒有使用過這個特定的設置。 – DatBassie

+0

在「while(b)」循環中b怎麼​​會是假的?什麼是B?這是一個錯誤嗎? – Upsilon42

相關問題