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;
}