Reputation: 776
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;
}
}
Upvotes: 2
Views: 3013
Reputation: 48602
You can Read/ Write your File in data/data/package_name/files Folder by,
To Write
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
File(getFilesDir()+File.separator+"MyFile.txt")));
bufferedWriter.write("lalit poptani");
bufferedWriter.close();
To Read
BufferedReader bufferedReader = new BufferedReader(new FileReader(new
File(getFilesDir()+File.separator+"MyFile.txt")));
String read;
StringBuilder builder = new StringBuilder("");
while((read = bufferedReader.readLine()) != null){
builder.append(read);
}
Log.d("Output", builder.toString());
bufferedReader.close();
To Read and write in external memory so that you can view your files visually. Read this tutorial here.
Upvotes: 0
Reputation: 1116
Unless and until your phone is rooted or you are using an emulator, I don't think you will able to see private files. Anyways, on the emulator you can always do an "adb shell" and do a "cd /data/data/package_name/" to have a look at the files.
One deprecated option is to set the flags as MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE in the mode section of the openFileOutput(String name, int mode) function.
Else if you want to access it through your file manager, you always have the option of storing non-private data on your phone's external storage. Have a look at the Save a File on External Storage section to know how to do that.
Upvotes: 1
Reputation: 96
You can use getExternalFilesDir to get the directory on the SDCard where you can write your files. Use null
as paramater for general files.
Path will be /mnt/sdcard/Android/data/[package]/files
Upvotes: 2
Reputation: 470
openFileOutput() func creates private file accesible by your application. Same as SharedPreferences file (private files). I means like the preference files are hidden but accessible by application in the same way your file is hidden and stored in private memory.. you can access from your code but not with file viewer..
Upvotes: 0