I am developing a simple Android application and I need to write a text file on the internal storage device. I know that there are many questions (and answers) on this question, but I really cannot understand what I am doing wrong.
This is part of the code that I use in my activity to write a file:
public void writeAFile(){
String fileName = "myFile.txt";
String textToWrite = "This is some text!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(fileName , Context.MODE_PRIVATE);
outputStream.write(textToWrite.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I really can't understand what mistake I am making. In addition, I tried this project on my emulator in Android Studio and on my phone to understand where I was doing something wrong, but even with this project, not a single file was written either on the phone or on the emulator.
EDIT:
, , :
public void ReadBtn(View v) {
try {
FileInputStream fileIn=openFileInput("myFile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
textmsg.setText(s);
} catch (Exception e) {
e.printStackTrace();
}
}
.