码迷,mamicode.com
首页 > 其他好文 > 详细

SDCard中文件存取的实现

时间:2014-07-18 13:14:00      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:拼接

注意:

第一步要先添加权限:

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

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

步骤:

         文件流

         1、写:

                   a)创建文件输出流(PATH

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

                   bb)关闭流

         2、读:

                   a)创建文件输入流

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

                   c)创建字节数组buffer1024)(建立变量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();


SDCard中文件存取的实现,布布扣,bubuko.com

SDCard中文件存取的实现

标签:拼接

原文地址:http://7990064.blog.51cto.com/7980064/1439690

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!