I am new to Android programming and so kingly pardon me if this question sounds stupid. I want to read and write from a file in an Android Program. I am aware of how to do that in Java.
Having read the documentation I wrote the following code. However I am unable to locate where this file is. According to the documentation it should be in folder data/data/package_name.
I have Samsung Galaxy S2. I launched the application, then closed it. When I went looking for the file I could not find it. I looked in My Files/data. But there is no data folder inside this data folder. What am I doing wrong?
Thank you for the help.
Sources: 1. http://developer.android.com/training/basics/data-storage/files.html 2. http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html#overview
public class MainActivity extends Activity {
private void writeFileToInternalStorage() {
String eol = System.getProperty("line.separator");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myfile.txt", MODE_WORLD_WRITEABLE)));
writer.write("This is a test1." + eol);
writer.write("This is a test2." + eol);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writeFileToInternalStorage();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}