SDCard中文件存取的实现-创新互联-成都快上网建站

SDCard中文件存取的实现-创新互联

注意:

创新互联专注于网站建设|网站建设维护|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖玻璃钢雕塑等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身开发品质网站。

第一步要先添加权限:

"android.permission.READ_EXTERNAL_STORAGE"/>

"android.permission.WRITE_EXTERNAL_STORAGE"/>

步骤:

        文件流

        1、写:

                  a)创建文件输出流(PATH)

                  b)将内容写入文件输出流(str.getBytes())

                  bb)关闭流

        2、读:

                  a)创建文件输入流

                  b)创建字节数组缓存(ByteArrayBufferarrayBuffer = new ByteArrayBuffer(1000);)——>为了后面分段读取时拼接数据

                  c)创建字节数组buffer(1024)(建立变量len=0)

                  d)循环读入——while(-1!=(len=fis.read(buffer)){arrayBuffer.append(buffer, 0, len) }

                  e)通过创建String的形式将字节数组转换成字符串(new String(arrayBuffer.toByteArray()))

                  f)将内容显示在文本框中

                  ff)关闭流

        对象流

        1、写:

                  a)创建对象

                  b)创建文件输出流(PATH)

                  c)创建对象输出流(ObjectOutputStream,将文件输出流嵌套在里面)

                  d)将对象写入对象输出流(oos.writeObject(person);)

                  dd)关闭最外层流(对象流)

        2、读:

                  a)创建文件输入流

                  b)创建对象输入流

                  c)将读到的Object对象强转成所需对象(Personperson = (Person)ois.readObject();)

                  d)在文本框中显示对象的各项内容

                  dd)关闭最外层流(对象流)

代码实现(文件流):

1、往sdcard中写入数据

        FileOutputStreamfos = null;

                  try{

                           fos= new FileOutputStream(PATH);

                           Stringstr = writeToSdcard.getText().toString();

                           //将字符串打散写入流中

                           fos.write(str.getBytes());

                  }catch (FileNotFoundException e) {

                           //TODO Auto-generated catch block

                           e.printStackTrace();

                  }catch (IOException e) {

                           //TODO Auto-generated catch block

                           e.printStackTrace();

                  }finally{

                           if(fos!=null){

                                    try{

//如果path有问题,会跳到finally中,而fos此时为null,会发生空指针异常

                                              fos.close();

                                    }catch (IOException e) {

                                              //TODO Auto-generated catch block

                                              e.printStackTrace();

                                    }

                           }

                  }

2、从sdcard中读取数据

        FileInputStreamfis = null;

                  //1000表示的是预留空间

                  ByteArrayBufferarrayBuffer = new ByteArrayBuffer(1000);

                  try{

                           fis= new FileInputStream(PATH);

                           byte[]buffer = new byte[1024];

                           intlen = 0;

                           while(-1!=(len=fis.read(buffer))){

                                    //拼接

                                    arrayBuffer.append(buffer,0, len);

                           }

                           //通过新建字符串的形式将arrayBuffer转换成字节数组

                           StringreadBuffer = new String(arrayBuffer.toByteArray());

                           writeToSdcard.setText(readBuffer);

                  }catch (FileNotFoundException e) {

                           //TODO Auto-generated catch block

                           e.printStackTrace();

                  }catch (IOException e) {

                           //TODO Auto-generated catch block

                           e.printStackTrace();

                  }finally{

                           if(fis!=null){

                                    try{

                                              fis.close();

                                    }catch (IOException e) {

                                              //TODO Auto-generated catch block

                                              e.printStackTrace();

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:SDCard中文件存取的实现-创新互联
本文路径:http://kswjz.com/article/dgghgp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流