Mattia
Mattia

Reputation: 329

Internal Storage and Files?

I'm trying to create a file in the internal storage

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

But when i search in /data/data/ i don't find the app dir to see the file presence...why? I try with a context.getApplicationInfo().dataDir() but it don't work it give error. How can i do? What is/are my mistake(s)?

Upvotes: 2

Views: 202

Answers (3)

Hades
Hades

Reputation: 3936

Try something like this,

File path = getFilesDir();
File file = new File(path, filename);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(string.getBytes());
outputStream.close();

you should probably do a google search before posting.

The file will be located in

data->data->package name->files->filename

Upvotes: 1

Christopher Francis
Christopher Francis

Reputation: 1

outputStream = context.getFilesDir().openFileOutput(filename, Context.MODE_PRIVATE);

getFilesDir() returns the Absolute path to the file .

Read here

Upvotes: 0

greenapps
greenapps

Reputation: 11224

File Explorers have no access to the /data directory.

With getFilesDir() you will know the directory where you put your file.

Upvotes: 0

Related Questions