Using emulator I am able to write a text file to /data/local but when I use my device, for the same path /data/local I get Permission denied. Could anyone tell me which path of Internal Storage is read-write and I can write my file there. It would be better if I could achieve this without rooting my device.
2 Answers
Take a look at this guide from the Android docs.
Calling getFilesDir
will return a File
object for your app's internal directory.
You can use that to write a new file in the directory like this
File file = new File(context.getFilesDir(), fileName);
Here's an answer with an example of how to write text to a file using getFilesDir
: https://stackoverflow.com/a/9306962/2278598
Or, you can use openFileOutput
, like this
String fileName = "yourFileName";
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();
}

- 1
- 1

- 12,073
- 8
- 39
- 55
Android apps are isolated one to another, so your app has a dedicated folder in internal storage to read/write into it.
You can access it via
File path = Environment.getDataDirectory();
As it vary between devices.
Outside the app (e.g. from a shell, or a file explorer) you can't even read private data folders of the apps, you need a rooted device (as the emulator) to do it. If you want a file to be world-readable, put it in the external storage.

- 3,365
- 5
- 35
- 52
-
Ok when I print the Log, the value of path is /data. So I can place my file manually in data folder? and manually declared read file from /data? – FAZ Jan 07 '16 at 15:37
-
Inside /data/
, and within your app, you can read/write. If you aren't using a rooted device and want to get the log file without your app, you need to put that log in the external storage, [as seen here](http://developer.android.com/intl/es/reference/android/os/Environment.html#getExternalStorageDirectory()) – webo80 Jan 07 '16 at 15:39 -
Currenly in my /data/ folder there is no app folder. Should i create it? If i create it, then no need to root the device? – FAZ Jan 07 '16 at 15:43
-
NO. The folder exists, but you (as non-root user) can't even see that exists. This is for security purposes, to allow your app's data remain private. When you root your device, you lose that protection. – webo80 Jan 07 '16 at 15:45
-
Ah ok. I actually wanted to achieve this without rooting but thankyou for your time and help :) – FAZ Jan 07 '16 at 15:46
-
Easy. If you want /data, unless you're rooted, you can't access it externally. If you want to access it, you must put it on the external storage. Glad to help you, please mark this answer as accepted if I helped – webo80 Jan 07 '16 at 15:48