亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

中阳县| 马公市| 如皋市| 稻城县| 安新县| 广安市| 郯城县| 高邮市| 黔西| 汶川县| 上虞市| 奉化市| 容城县| 新晃| 无锡市| 凤翔县| 新河县| 宜章县| 日土县| 西乌| 桐乡市| 巩义市| 眉山市| 塘沽区| 双鸭山市| 扶绥县| 崇左市| 江口县| 美姑县| 奇台县| 朝阳区| 安康市| 田林县| 浮梁县| 怀来县| 翼城县| 晴隆县| 扶沟县| 岢岚县| 乐业县| 太谷县|