I am just a beginner in learning Android.I want to learn how to write a piece of text to a file in Android.
My code to write, on clicking a button looks like this:
write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FileOutputStream fos;
try {
fos = openFileOutput("filename", Context.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tv.setText("Values saved");
}
});
When I execute this the FileNotFoundException
is thrown in logcat. As far as I know a new file will be created if no file with such name exists.
The logcat message is:
> 05-14 08:37:55.085: W/System.err(281): java.io.FileNotFoundException: /data/data/com.example.myproject11/files/test (No such file or directory)