tags:

views:

113

answers:

1

I have some problems with the creation of a file. For instance, I want to create a file on the sdcard and first i want to check whether file exists or not. If file not exist i will create one and write some text in otherwise if it exists i will append it some text.

+2  A: 
     String state = Environment.getExternalStorageState();

                if (Environment.MEDIA_MOUNTED.equals(state)) 
                {
                     //SDcard is available
                       File f=new File("/sdcard/test.txt");
                       if (!f.exists()) 
                       {
                        //File does not exists
                        f.createNewFile();
                       }

                      //take your inputstream and write it to your file

                      OutputStream out=new FileOutputStream(f);
                      byte buf[]=new byte[1024];
                      int len;
                      while((len=inputStream.read(buf))>0)
                      out.write(buf,0,len);
                      out.close();
                      inputStream.close();
                      System.out.println("\nFile is created...................................");


                }

Dont forget to add the following permission to manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Rahul
Great! oooh thank for your answering.
Curely Nara
if you are satisfied with the answer select the tick mark that is available on the left hand side. It means that you have accepted the answer.
Rahul