温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SDCard中文件存取的实现

发布时间:2020-06-27 18:56:13 来源:网络 阅读:298 作者:小诚诚cc 栏目:开发技术

注意:

第一步要先添加权限:

<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();


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI