0

我正在创建一个小应用程序,它可以让用户读取 /data/local 中某个文本文件中的内容,对其进行编辑,然后保存。通过在这里和那里使用一些教程,我已经让一切正常工作,但仍然有一些东西不起作用。

已经实现了 Root 访问权限,并且写入/读取文件也完成了,但是当按下“写入”按钮时,我得到一个祝酒词“打开失败:EACCES(权限被拒绝)”。不幸的是,谷歌在这方面对我帮助不大。另外,我正在使用 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

}

提前致谢,

巴斯

4

1 回答 1

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());

 }


}
于 2012-05-22T18:01:21.707 回答