Writing and Reading String to an internal storage in Android
Asked Answered
B

2

6

I want to write in a file and then read from it. While using openFileOutput(..,..) method I get that this method is not defined as it is an abstract method. Then I tried it with passing the context using getBaseContext(); and warning was off but I am not getting the result on reading the output. I also tried passing the context as a parameter in the constructor but that also did not help. I want to write static methods so that I don't have to instantiate the class every time and static is not the reason because I have tried without it also. The code is snippet is given below.

Do I need to specify any path even while using internal storage? Is there any permission required to write files on internal storage? (I have included the permission to write on external storage)

public static void write (String filename,Context c,String string) throws IOException{
    try {
        FileOutputStream fos =  c.openFileOutput(filename, Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public static String read (String filename,Context c) throws IOException{

    StringBuffer buffer = new StringBuffer();

    FileInputStream fis = c.openFileInput(filename);
    BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
    if (fis!=null) {                            
        while ((Read = reader.readLine()) != null) {    
            buffer.append(Read + "\n" );
        }               
    }       
    fis.close();
    return Read;
}
Bolger answered 28/8, 2012 at 10:12 Comment(4)
You want to return buffer.toString() in read()! (BTW, "String Read;" seems to be missing in your code snippet, however you should not use uppercase non-static variables.)Andro
So how can I do that? A code snippet will possibly help.Bolger
return buffer.toString() as last line in your read() method. And add "String Read;" somewhere before your while-loop to define the "Read" variable. And you should use lower case, e.g. String line; while( (line = reader.readLine()) != null) buffer.append(line + "\n");Andro
Thanks! I actually defined Read as a static variable in my class.Bolger
B
2

return buffer.toString() in read method solved the problem. Thanks Stefan.

Bolger answered 3/10, 2012 at 6:49 Comment(1)
Sorry for making this thread live again after a long time. I read a file from asset folder and then wrote that file using FileOutputStream. My question is what the path of the file saved by FileOutputStream.Gallium
C
1

Is there any permission required to write files on internal storage?

"You don’t need any permissions to save files on the internal storage. Your application always has permission to read and write files in its internal storage directory."

You may see some posts stating to use: "android.permission.WRITE_INTERNAL_STORAGE"

There is no such permission; "WRITE_INTERNAL_STORAGE"

Condescendence answered 29/7, 2013 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.