DatBassie
DatBassie

Reputation: 312

Writing files to and reading from /data/local?

I am creating a little app that will let a user read what's in a certain textfile in /data/local, edit it, and then save it. I have gotten everything to work by using some tutorials here and there, but there's still something not working.

Root access has been achieved, and writing/reading the file is done too, but when pressing the "Write" button, I get a toast saying "open failed: EACCES (Permission denied)". Google unfortunately didn't help me much on this one. Also, I am using the WRITE_TO_EXTERNAL_STORAGE permission.

Code:

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

}

Thanks in advance,

Bas

Upvotes: 0

Views: 1434

Answers (1)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Try this logic for writing and reading the file on local storage, and just a remainder, though i know u did it, but still..use WRITE_TO_EXTERNAL_STORAGE permission.

For Reading from a file

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

For Writing to a file:

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

 }


}

Upvotes: 1

Related Questions