开发者

Create file in android emulator

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 o开发者_如何转开发r not. If file not exist i will create one and write some text in otherwise if it exists i will append it some text.


     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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜