• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps call read and write file in class from fragment over mainActivity

alex_92

Lurker
hi,
after wasting a complete evening of useless trying I really need some help.
I have an Activity with fragments and want to write and read files by clicking an a button or any other event from a fragment.
The problem is, that I have to place the read and write methods in the mainActivity to get all variables. But I can't call them because the static/non-static problem. I don't know how to fix this. And when I try to put the mothods to a extern class it doesn't compile because "openFileOutput()" is undefined. Here is my code that worked in an app without fragments.

[HIGH]
public void writeData (String s)
{
BufferedWriter bufferWriter = null;
try {
FileOutputStream fileOutputStream = openFileOutput("testFile", Context.MODE_PRIVATE);
bufferWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferWriter.write(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public String readData()
{
BufferedReader bufferReader = null;
StringBuilder result = new StringBuilder();
try {
FileInputStream fileInputStream = openFileInput("testFile");
bufferReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line;
while ((line = bufferReader.readLine()) != null) {
result.append(line + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bufferReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("fileAccess", "" + result.toString());
return (result.toString());
}
[/HIGH]
 
There are probably an infinite number of ways to solve this problem, but here's the absolute simplest:

Put this class anywhere in the project.

public static class FileInterface {
public static void writeData (Context c, String s)
{
BufferedWriter bufferWriter = null;
try {
FileOutputStream fileOutputStream = c.openFileOutput("testFile", Context.MODE_PRIVATE);
bufferWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
bufferWriter.write(s);
bufferWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static String readData(Context c)
{
BufferedReader bufferReader = null;
StringBuilder result = new StringBuilder();
try {
FileInputStream fileInputStream = c.openFileInput("testFile");
bufferReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line;
while ((line = bufferReader.readLine()) != null) {
result.append(line + "\n");
}
bufferReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return (result.toString());
}
}
Then call it like this, from within your activity or fragment:
FileInterface.writeData(getApplicationContext(), "some string");
The application context should be appropriate for what you're trying to do.

I wish this forum didn't have broken code formatting. Really annoying.
 
first thank you, for your hep.
but your way isn't possible.
Because the file access methods like openFileOutput need the context of the main-activity. But I can't pass the context from a static function.
This was my problem.
But I solved it.
I put a variable in the file access class to store the context, which is passed at the onCreate of the main-activity over the constructor of the class.
This way I don't have to pass the context when calling the file access methods.
Now I can read and write files from fragment events.
 
Back
Top Bottom