91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

收藏| 株洲市| 德江县| 灯塔市| 八宿县| 闻喜县| 开封县| 丹阳市| 松阳县| 浪卡子县| 遂宁市| 浦北县| 万安县| 永登县| 府谷县| 吉安市| 布尔津县| 天台县| 钦州市| 股票| 松原市| 桐柏县| 青神县| 定日县| 酒泉市| 冀州市| 和林格尔县| 太谷县| 长子县| 双江| 福海县| 德昌县| 施甸县| 广州市| 富阳市| 鞍山市| 西乡县| 虞城县| 霍林郭勒市| 阿克陶县| 安福县|