1

I tried a simple file write program in Android.

 try 
            {
                //File f1=new File("/sdcard/Prag.txt");
                File f1=new File("J:\\Prag.txt");
                BufferedWriter out = new BufferedWriter(new FileWriter(f1,true));
                out.write("Pragadheesh" + "\n");
                out.close();

                } catch (IOException ioe) 
                  {ioe.printStackTrace();}

But the exception is getting hit. How can I fix this?

[edit]: I tried File f1=new File(Environment.getExternalStorageDirectory() + "//Prag.txt"); and f1 value = /mnt/sdcard/Prag.txt But still the exception gets hit. I have changed the permission too in manifest file

[solution] As per Krishnabhadra's reply, I had to Make sure your SDCard is not mounted on a PC

SyncMaster
  • 9,754
  • 34
  • 94
  • 137

5 Answers5

9

Android is based on linux so windows style paths like "J:\\prag.txt" won't work. In linux paths are like this: "/folder1/folder2/file.txt"

Also don't use hardcoded paths, because different phones might have different path for the SD card. Therefore you you should use Environment.getExternalStorageDirectory() that gives the path to the SD card programmatically.

So use:

File f1=new File(Environment.getExternalStorageDirectory() + "Prag.txt");

Also add necessary permission by:

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

Inside your manifest, somewhere outside application tag.

Caner
  • 57,267
  • 35
  • 174
  • 180
  • I tried File f1=new File(Environment.getExternalStorageDirectory() + "//Prag.txt"); and f1 value = /mnt/sdcard/Prag.txt But still the exception gets hit. I have changed the permission too in manifest file – SyncMaster Nov 18 '11 at 11:30
  • What does the exception say?(post the stacktrace also) – Caner Nov 18 '11 at 11:41
3

All the above answers are useful..But remember one more thing..Make sure your SDCard is not mounted on a PC. Only one device can access SDCard at one time. ie either the computer or the phone can access the SD card, but not both at the same time..If it is mounted on PC then your code won't work..See this thread

Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
2

Replace:

File f1=new File("J:\\Prag.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f1,true));

with:

BufferedWriter out = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory(),"prag.txt"))
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

Have you set the required permission in your manifest?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
JimmyB
  • 12,101
  • 2
  • 28
  • 44
1

what is this path "J:\Prag.txt" ; Android doesn't recognize any drive path like system.

Use sdcard path for it.

File f1=new File(Environment.getExternalStorageDirectory()+"/Prag.txt");

Also mention permission in manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I tried File f1=new File(Environment.getExternalStorageDirectory() + "//Prag.txt"); and f1 value = /mnt/sdcard/Prag.txt But still the exception gets hit. I have changed the permission too in manifest file – SyncMaster Nov 18 '11 at 11:31
  • As per Krishnabhadra's reply, I had to Make sure your SDCard is not mounted on a PC – SyncMaster Nov 18 '11 at 11:38
  • but if you post error log then it is easy to us figure-out what's wrong here.What's the real problem? – user370305 Nov 18 '11 at 11:42