• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            life02

              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              197 隨筆 :: 3 文章 :: 37 評(píng)論 :: 0 Trackbacks
            http://www.cnblogs.com/shaoyangjiang/archive/2012/3/9.html

            Android 文件管理方法
            Android使用的是基于Linux的文件系統(tǒng),對(duì)于文件的訪問(wèn)和管理是通過(guò)權(quán)限設(shè)置來(lái)限制的.
            在Linux系統(tǒng)中,文件權(quán)限分別描述了創(chuàng)建者、同組用戶和其他用戶對(duì)文件的操作限制。
            x表示可執(zhí)行,r表示可讀,w表示可寫(xiě),d表示目錄,
            -表示普通文件。
            產(chǎn)生這樣的文件權(quán)限與程序人員設(shè)定的
            Android 存儲(chǔ)文件的類型
            (內(nèi)部存儲(chǔ))程序開(kāi)發(fā)人員可以建立和訪問(wèn)程序自身的私有文件;
            (資源存儲(chǔ))可以訪問(wèn)保存在資源目錄中的原始文件和XML文件;
            (外部存儲(chǔ))可以在SD卡等外部存儲(chǔ)設(shè)備中保存文件

            Android系統(tǒng)允許應(yīng)用程序創(chuàng)建僅能夠自身訪問(wèn)的私有文件,文件保存在設(shè)備的內(nèi)部存儲(chǔ)器上,在Linux系統(tǒng)下的
            /data/data/<package name>/files目錄中
            Android系統(tǒng)不僅支持標(biāo)準(zhǔn)Java的IO類和方法,還提供了能夠簡(jiǎn)化讀寫(xiě)流式文件過(guò)程的函數(shù)
            FileOutputStream openFileOutput(String filename 
            int mode)
            FileInputStream openFileInput(String filename)
            參數(shù)文件不允許包含描述路徑的斜杠(其存儲(chǔ)位置固定)
            訪問(wèn)模式:

            MODE_PRIVATE 私有模式,缺陷模式,文件僅能夠被文件創(chuàng)建程序訪問(wèn),或具有相同UID的程序訪問(wèn)。
            MODE_APPEND 追加模式,如果文件已經(jīng)存在,則在文件的結(jié)尾處添加新數(shù)據(jù)。
            MODE_WORLD_READABLE 全局讀模式,允許任何程序讀取私有文件。
            MODE_WORLD_WRITEABLE 全局寫(xiě)模式,允許任何程序?qū)懭胨接形募?br />
            三個(gè)基本的讀方法

            abstract int read() :讀取一個(gè)字節(jié)數(shù)據(jù),并返回讀到的數(shù)據(jù),如果返回-1,表示讀到了輸入流的末尾。
               
            int read(byte[] b) :將數(shù)據(jù)讀入一個(gè)字節(jié)數(shù)組,同時(shí)返回實(shí)際讀取的字節(jié)數(shù)。如果返回-1,表示讀到了輸入流的末尾。
               
            int read(byte[] b, int off, int len) :將數(shù)據(jù)讀入一個(gè)字節(jié)數(shù)組,同時(shí)返回實(shí)際讀取的字節(jié)數(shù)。如果返回-1,表示讀到了輸入流的末尾。off指定在數(shù)組b中存放數(shù)據(jù)的起始偏移位置;len指定讀取的最大字節(jié)數(shù)。
            其它方法
              
            long skip(long n) :在輸入流中跳過(guò)n個(gè)字節(jié),并返回實(shí)際跳過(guò)的字節(jié)數(shù)。
              
            int available() :返回在不發(fā)生阻塞的情況下,可讀取的字節(jié)數(shù)。
              
            void close() :關(guān)閉輸入流,釋放和這個(gè)流相關(guān)的系統(tǒng)資源。
              
            void mark(int readlimit) :在輸入流的當(dāng)前位置放置一個(gè)標(biāo)記,如果讀取的字節(jié)數(shù)多于readlimit設(shè)置的值,則流忽略這個(gè)標(biāo)記。
              
            void reset() :返回到上一個(gè)標(biāo)記。
              
            boolean markSupported() :測(cè)試當(dāng)前流是否支持mark和reset方法。如果支持,返回true,否則返回false。

            三個(gè)基本的寫(xiě)方法

            abstract void write(int b) :往輸出流中寫(xiě)入一個(gè)字節(jié)。
               
            void write(byte[] b) :往輸出流中寫(xiě)入數(shù)組b中的所有字節(jié)。
               
            void write(byte[] b, int off, int len) :往輸出流中寫(xiě)入數(shù)組b中從偏移量off開(kāi)始的len個(gè)字節(jié)的數(shù)據(jù)。
            其它方法
              
            void flush() :刷新輸出流,強(qiáng)制緩沖區(qū)中的輸出字節(jié)被寫(xiě)出。
              
            void close() :關(guān)閉輸出流,釋放和這個(gè)流相關(guān)的系統(tǒng)資源。

            對(duì)文件和流的操作容易引發(fā)異常,所以必須要用try
            -catch語(yǔ)句

            主要核心代碼

            首先是新建一個(gè)文件
            private final String FILE_NAME = "Myfile01.txt";
            寫(xiě)文件
            FileOutputStream fos 
            = null;//聲明一個(gè)全局變量
            //注意下面的語(yǔ)句要進(jìn)行拋異常處理FileNotFoundException e,IOException e
            fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);//寫(xiě)流式文件過(guò)程的

            函數(shù),這里的權(quán)限是私有的
            String text 
            = entryText.getText().toString();//把輸入的內(nèi)容轉(zhuǎn)化為字符串
            fos.write(text.getBytes());//把轉(zhuǎn)化為字符串的內(nèi)容轉(zhuǎn)化為字節(jié),然后寫(xiě)入
            //下面語(yǔ)句寫(xiě)在finally里面
            fos.flush();//把緩存里的內(nèi)容寫(xiě)入到文件
            fos.close();//關(guān)閉流

            讀文件
            FileInputStream fis 
            = null;//定義一個(gè)全局變量
            fis = openFileInput(FILE_NAME);//打開(kāi)要讀取的文件
            if (fis.available() == 0){//判斷文件是否為空,為空就直接返回
             return;
            }
            byte[] readBytes = new byte[fis.available()];//把文件里的內(nèi)容轉(zhuǎn)化為字節(jié)
            while(fis.read(readBytes) != -1){//讀文件,直到讀到最后
            }
            String text 
            = new String(readBytes);//把讀到的字節(jié)轉(zhuǎn)化為字符串

            讀文件的第二種方法

            String path 
            = "/data/data/cn.itcast.file/files/writeable.txt";//得到文件路徑
              File file = new File(path);//創(chuàng)建一個(gè)文件對(duì)象
              FileInputStream inStream = new FileInputStream(file);//讀文件
              byte[] buffer = new byte[1024];//緩存
              int len = 0;
              ByteArrayOutputStream outStream 
            = new ByteArrayOutputStream();
              
            while( (len = inStream.read(buffer))!= -1){//直到讀到文件結(jié)束
               outStream.write(buffer, 0, len);
              }
              
            byte[] data = outStream.toByteArray();//得到文件的二進(jìn)制數(shù)據(jù)
              outStream.close();
              inStream.close();
              Log.i(TAG, 
            new String(data));

            這里列舉一個(gè)例子,主要是把寫(xiě)入的文件讀出來(lái)顯示在TextView中

            第一步,修改布局文件main.xml
            View Code

             
            1 <?xml version="1.0" encoding="utf-8"?>
             
            2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             
            3     android:orientation="vertical"
             
            4     android:layout_width="fill_parent"
             
            5     android:layout_height="fill_parent"
             
            6     >
             
            7     <TextView  android:id="@+id/label"
             
            8     android:layout_width="fill_parent" 
             
            9     android:layout_height="wrap_content" 
            10     android:text="@string/hello"
            11     />
            12     <EditText android:id="@+id/entry"
            13         android:text="輸入文件內(nèi)容"  
            14         android:layout_width="fill_parent" 
            15         android:layout_height="wrap_content">
            16     </EditText>
            17     <LinearLayout android:id="@+id/LinearLayout01" 
            18         android:layout_width="wrap_content" 
            19         android:layout_height="wrap_content">
            20         <Button android:id="@+id/write"
            21             android:text="寫(xiě)入文件"  
            22             android:layout_width="wrap_content" 
            23             android:layout_height="wrap_content">
            24         </Button>
            25         <Button android:id="@+id/read"
            26             android:text="讀取文件"  
            27             android:layout_width="wrap_content" 
            28             android:layout_height="wrap_content">
            29         </Button>
            30     </LinearLayout>
            31     <CheckBox android:id="@+id/append"
            32         android:text="追加模式"  
            33         android:layout_width="wrap_content" 
            34         android:layout_height="wrap_content">
            35     </CheckBox>
            36     <TextView android:id="@+id/display"
            37         android:text="文件內(nèi)容顯示區(qū)域"  
            38         android:layout_width="fill_parent" 
            39         android:layout_height="fill_parent"
            40         android:background="#FFFFFF" 
            41         android:textColor="#000000" >
            42     </TextView>
            43 </LinearLayout>

            復(fù)制代碼

            第二步:寫(xiě)入核心代碼,
            View Code

              
            1 package cn.edu.zwu.tel;
              
            2 
              
            3 import java.io.FileInputStream;
              
            4 import java.io.FileNotFoundException;
              
            5 import java.io.FileOutputStream;
              
            6 import java.io.IOException;
              
            7 import android.app.Activity;
              
            8 import android.content.Context;
              
            9 import android.os.Bundle;
             
            10 import android.view.View;
             
            11 import android.view.View.OnClickListener;
             
            12 import android.widget.Button;
             
            13 import android.widget.CheckBox;
             
            14 import android.widget.EditText;
             
            15 import android.widget.TextView;
             
            16 
             
            17 public class FileSaveTest01Activity extends Activity {
             
            18     
             
            19     private final String FILE_NAME = "Myfile01.txt";
             
            20     private TextView labelView;
             
            21     private TextView displayView;
             
            22     private CheckBox appendBox ;
             
            23     private EditText entryText;
             
            24     @Override
             
            25     public void onCreate(Bundle savedInstanceState) {
             
            26         super.onCreate(savedInstanceState);
             
            27         setContentView(R.layout.main);
             
            28         
             
            29         labelView = (TextView)findViewById(R.id.label);
             
            30         displayView = (TextView)findViewById(R.id.display);
             
            31         appendBox = (CheckBox)findViewById(R.id.append);
             
            32             entryText = (EditText)findViewById(R.id.entry);
             
            33         Button writeButton = (Button)findViewById(R.id.write);
             
            34         Button readButton = (Button)findViewById(R.id.read);
             
            35         writeButton.setOnClickListener(writeButtonListener);
             
            36         readButton.setOnClickListener(readButtonListener);
             
            37         entryText.selectAll();
             
            38         entryText.findFocus();
             
            39     }
             
            40     
             
            41     
             
            42     OnClickListener writeButtonListener = new OnClickListener() { 
             
            43         @Override
             
            44         public void onClick(View v) {
             
            45             FileOutputStream fos = null;    
             
            46             try {
             
            47                  if (appendBox.isChecked()){
             
            48                      fos = openFileOutput(FILE_NAME,Context.MODE_APPEND);
             
            49                  }
             
            50                  else {
             
            51                      fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
             
            52                  }
             
            53                     
             
            54                  String text = entryText.getText().toString();
             
            55                  fos.write(text.getBytes());
             
            56                  labelView.setText("文件寫(xiě)入成功,寫(xiě)入長(zhǎng)度:"+text.length());
             
            57                  entryText.setText("");
             
            58             } catch (FileNotFoundException e) {
             
            59                 e.printStackTrace();
             
            60             }
             
            61             catch (IOException e) {
             
            62                 e.printStackTrace();
             
            63             }
             
            64             finally{
             
            65                 if (fos != null){
             
            66                     try {
             
            67                         fos.flush();
             
            68                         fos.close();
             
            69                     } catch (IOException e) {
             
            70                         e.printStackTrace();
             
            71                     }            
             
            72                 }
             
            73             }
             
            74         }   
             
            75      };
             
            76      
             
            77      OnClickListener readButtonListener = new OnClickListener() { 
             
            78          @Override
             
            79          public void onClick(View v) {
             
            80              displayView.setText("");
             
            81              FileInputStream fis = null;
             
            82              try {
             
            83                 fis = openFileInput(FILE_NAME);
             
            84                 if (fis.available() == 0){
             
            85                     return;
             
            86                 }
             
            87                 byte[] readBytes = new byte[fis.available()];
             
            88                 while(fis.read(readBytes) != -1){
             
            89                 }
             
            90                 String text = new String(readBytes);
             
            91                 displayView.setText(text);
             
            92                 labelView.setText("文件讀取成功,文件長(zhǎng)度:"+text.length());
             
            93             } catch (FileNotFoundException e) {
             
            94                 e.printStackTrace();
             
            95             }
             
            96             catch (IOException e) {
             
            97                 e.printStackTrace();
             
            98             }
             
            99              
            100          }   
            101       };
            102      
            103 }

            復(fù)制代碼

            效果圖:

            posted on 2012-04-12 13:38 life02 閱讀(2672) 評(píng)論(1)  編輯 收藏 引用 所屬分類: Android開(kāi)發(fā)

            評(píng)論

            # re: android SD卡文件的讀寫(xiě)(z轉(zhuǎn)載) 2012-04-17 23:17 brief
            不錯(cuò),有幫助  回復(fù)  更多評(píng)論
              

            亚洲国产精品成人久久| 无码人妻久久一区二区三区蜜桃| 精品综合久久久久久98| 久久综合丁香激情久久| 中文字幕精品无码久久久久久3D日动漫| 久久人人添人人爽添人人片牛牛| 亚洲午夜久久久久久噜噜噜| 久久狠狠一本精品综合网| 狠狠色丁香婷综合久久| 久久婷婷国产综合精品| 久久青青草视频| 欧美激情精品久久久久久久九九九| 久久亚洲精品视频| 久久精品视频一| 国内精品久久久久久久coent| 久久精品九九亚洲精品天堂 | 精品亚洲综合久久中文字幕| 久久国产精品一区| 韩国免费A级毛片久久| 久久精品一本到99热免费| 色综合久久无码五十路人妻| 久久九九久精品国产| 少妇久久久久久久久久| 婷婷国产天堂久久综合五月| 91超碰碰碰碰久久久久久综合| 久久久久四虎国产精品| 精品久久久无码人妻中文字幕| 久久久久久国产精品无码下载| 91精品国产91久久综合| 久久成人精品视频| 狠狠色丁香久久婷婷综合五月| 久久精品aⅴ无码中文字字幕不卡| 国产午夜精品久久久久九九| 99国产精品久久久久久久成人热| 久久久久久久精品妇女99| 久久久久亚洲AV无码观看| 久久笫一福利免费导航 | 欧美伊人久久大香线蕉综合| 久久久国产精品| 久久受www免费人成_看片中文| 污污内射久久一区二区欧美日韩|