M Azeem N
M Azeem N

Reputation: 933

How to write to an external text file in Android?

I want to write a string line in a text file in android project (using Eclipse). This is the code which i am trying but its not working...

    String s="hello azeem\n";
    try{
    FileOutputStream fos = openFileOutput("C:\\eventslog.txt", Context.MODE_PRIVATE);
    fos.write(s.getBytes());
    fos.close();
    }
    catch (Exception e){

    }

Any ideas??

Upvotes: 0

Views: 13473

Answers (3)

Cosmic Bacon
Cosmic Bacon

Reputation: 301

Write the text in Android to the device's sdcard. Use Android Bridge (adb.exe found in sdk\platform-tools) to pull the txt file onto your drive... in your case, a windows disk.

(write text to foo.txt in android)

in a command bar.bat:

adb pull /sdcard/Android/data/com.example.yourapp/files/foo.txt C:\foo.txt

Run the bar.bat file. That's it. foo.txt now exists on the sdcard and on C:\foo.txt

Sorry if this does not match your intent. You must invoke the batch file from Windows.

Upvotes: 1

Mohsen Bahman
Mohsen Bahman

Reputation: 1092

Perhaps you didn't add its permission...

this is here:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: -1

MByD
MByD

Reputation: 137412

There are two ways to understand your question:

  1. You want to write the file to c: drive in your computer - well you can't. If you want to save some file from android to your computer programmatic, you'll have to send it via network.
  2. You want to write to some place in the android FS - in this case c:\ is irreleveant, since this is a windows path, and android is unix based, meaning all paths start from root (/) also, android SDK provides you a few methods to access relevant directories files without using a fixed path. see here: http://developer.android.com/guide/topics/data/data-storage.html

Upvotes: 4

Related Questions