szpic
szpic

Reputation: 4496

Can't find created file

I'm writing my little app for Android. I'm trying to create files. Here is a code snippet:

final String TESTSTRING = new String("Hello Android");                 
FileOutputStream fOut = openFileOutput("samplefile.txt",Context.MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut); 
osw.write(TESTSTRING);
osw.flush();
osw.close();

FileInputStream fIn = openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[TESTSTRING.length()];

isr.read(inputBuffer);

String readString = new String(inputBuffer);

boolean isTheSame = TESTSTRING.equals(readString);

if(isTheSame) {
    TextView mLat=(TextView) findViewById(R.id.textView1);
    mLat.setText(String.valueOf(isTheSame));     
}   

I can't find this created samplefile.txt using file manager. Can anyone tell me the path where app is writing?

ok now i have this:

String pathToExternalStorage = Environment.getExternalStorageDirectory().toString();
     File appDirectory = new File(pathToExternalStorage + "/" + "testowa", "new_file.txt");
     try{
         OutputStream os = new FileOutputStream(appDirectory);
         os.write(pathToExternalStorage.getBytes());
         os.flush();
         os.close();
     }
     catch(IOException e)
     {

     }

Exception Thrown on OutputStream os = new FileOutputStream(appDirectory); Cant find why. i modified manifest with permissions

Upvotes: 0

Views: 5420

Answers (3)

John Paul Moore
John Paul Moore

Reputation: 76

The file is located in /data/data/com.yourpackagename/files/filename.txt which is hidden unless the device is rooted. You can access the file by copying it from the directory where it is created.

From the command line:

To copy a file

    adb -d shell 
    run-as com.yourpackagename 
    cat /data/data/com.yourpackagename/files/filename.txt > /sdcard/filename.txt

Alternatively for a SQLite database

    cat /data/data/com.yourpackagename/databases/dbname.db > /sdcard/dbname.sqlite 

This solution will work without having to root the device.

Upvotes: 1

Henry
Henry

Reputation: 43728

It is created in /data/data/your.package.name/files. However, this directory is not visible for file mangagers (unless they have root rights).

Upvotes: 0

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28697

You can get the path of the files created using openFileOutput(name) using getFileStreamPath(name). But file manager apps will need root privileges to view this path.

Upvotes: 0

Related Questions