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]
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]