创建于Android模拟器文件 [英] Create file in android emulator

查看:157
本文介绍了创建于Android模拟器文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,创建一个文件。例如,我想创建的SD卡一个文件,我想先检查是否文件存在与否。如果文件不存在,我将创建一个并另有写一些文字,如果它存在,我将其追加一些文本。

解决方案

 字符串状态= Environment.getExternalStorageState();

                如果(Environment.MEDIA_MOUNTED.equals(州))
                {
                     // SD卡可用
                       文件f =新的文件(/ SD卡/ test.txt的);
                       如果(!f.exists())
                       {
                        //文件不存在
                        f.createNewFile();
                       }

                      //把你的InputStream和其写入文件

                      的OutputStream OUT =新的FileOutputStream(F);
                      中byte buf [] =新的字节[1024];
                      INT LEN;
                      而((LEN = inputStream.read(BUF))大于0)
                      out.write(BUF,0,的len);
                      out.close();
                      inputStream.close();
                      的System.out.println(\ n文件被创建...................................);


                }
 

不要忘记添加以下权限体现

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E>< /使用-许可>
 

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.

解决方案

     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>

这篇关于创建于Android模拟器文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆