4

I have tried many solutions to read files but no one were working. I need a method to read a system file and show the text in a toast or in a dialog. Of course my app has root permission. I have to show the content of "eoc_status" in a toast after a checkbox click.

For example;

Runtime.getRuntime().exec("/sys/kernel/abb-chargalg/eoc_status").getInputStream(); 

I need to open text files.

mascIT
  • 413
  • 4
  • 16
  • I'm afraid your going to need to provide some example code for the kind of thing you've tried and be a little more specific with what you need and why. For example, what do you mean by a 'system' file? That way it will be easier for people to help you and your question will be more useful for other people looking for an answer to a similar question. – Rudi Kershaw Mar 26 '14 at 10:47
  • For example this: Runtime.getRuntime().exec("/system/bin/getprop").getInputStream(); I need opening text Files – mascIT Mar 26 '14 at 10:58
  • What file are you trying to retrieve? An **actual** system file (if so, which one?), or a file you have stored somewhere (if so, how did you store it?) in advance? – Rudi Kershaw Mar 26 '14 at 11:02
  • 1
    i have edited the main question. – mascIT Mar 26 '14 at 11:04
  • What are the permissions of the file in question? If an app process is not allowed to read it, you would need to launch a helper process using a hacked `su` shim or similar. – Chris Stratton Mar 26 '14 at 14:53

1 Answers1

7

Assuming you do have read-access to eoc_status

You are going to want to read it, not exec it. ie use cat or use a FileReader:

Then you will want to do something (put it in your toast) with the returned InputStream.

For example:

    BufferedReader  buffered_reader=null;
    try 
    {
        //InputStream istream = Runtime.getRuntime().exec("cat /sys/kernel/abb-chargalg/eoc_status").getInputStream();
        //InputStreamReader istream_reader = new InputStreamReader(istream);
        //buffered_reader = new BufferedReader(istream_reader);
        buffered_reader = new BufferedReader(new FileReader("/sys/kernel/abb-chargalg/eoc_status"));
        String line;

        while ((line = buffered_reader.readLine()) != null) 
        {
            System.out.println(line);
        }           
    } 
    catch (IOException e) 
    {
        // TODO 
        e.printStackTrace();
    }
    finally 
    {
        try 
        {
            if (buffered_reader != null)
                buffered_reader.close();
        } 
        catch (IOException ex) 
        {
            // TODO 
            ex.printStackTrace();
        }
    }       
violet313
  • 1,912
  • 1
  • 15
  • 19
  • 1
    There is no point to launching cat as an executable, as you could do the reading directly from java. Launching an executable only makes sense if you are doing it by way of a hacked `su` that might function as a shim to run cat as root, in which case it would be able to do things the java program could not. – Chris Stratton Mar 26 '14 at 11:57