I wrote two methods to write and read data from Internal Storage, now that I want to put them in a non activity class I get errors at openFileOutput
and openFileInput
that says the method openFileInput
/openFileOutput
is undefined for the type IOstream(name of my class)
I don't know how to fix it.
public void write(String fileName, String content) throws IOException{
FileOutputStream outStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
public String read(String fileName) throws IOException{
FileInputStream inStream = openFileInput(fileName);
String content = null;
byte[] readByte = new byte[inStream.available()];
while(inStream.read(readByte) != -1){
content = new String(readByte);
}
inStream.close();
return content;
}
I'm looking for a way to put these methods in their own class
When you post a question and say "I get errors", actually posting said errors would go a long way to show people your problem.
Taking a guess from your question and code, you've moved those methods to a class that does not extend Context, where those two methods are declared, so they can't be found.
You need a reference to a context to be able to access those methods.
public void write(Context context, String fileName, String content) throws IOException{
FileOutputStream outStream = context.openFileOutput(fileName, Context.MODE_PRIVATE);
outStream.write(content.getBytes());
outStream.close();
}
public String read(Context context, String fileName) throws IOException{
FileInputStream inStream = context.openFileInput(fileName);
String content = null;
byte[] readByte = new byte[inStream.available()];
while(inStream.read(readByte) != -1){
content = new String(readByte);
}
inStream.close();
return content;
}