国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Android development manual / 數(shù)據(jù)存儲與訪問之——文件存儲讀寫

數(shù)據(jù)存儲與訪問之——文件存儲讀寫

本節(jié)引言:

嘿嘿,看到這個(gè)題目,相信部分讀者會問,你前面的Fragment寫完了嗎?嗯,沒寫完,因?yàn)橄肜?,需?一點(diǎn)時(shí)間,為了提高效率,所以決定像多線程一樣,并發(fā)的來寫教程,這樣可能可以加快寫教程的進(jìn)度, 到現(xiàn)在為止,剛好寫了60篇,離完成入門教程還很遠(yuǎn)呢,而前面也說過,想在一個(gè)半到兩個(gè)月之內(nèi)完成 這套教程,今天已經(jīng)9.1號了,要加吧勁~好的,廢話就這么多,本節(jié)給大家介紹的是Android數(shù)據(jù)存儲與 訪問方式中的一個(gè)——文件存儲與讀寫,當(dāng)然除了這種方式外,我們可以存到SharedPreference,數(shù)據(jù)庫, 或者Application中,當(dāng)然這些后面都會講,嗯,開始本節(jié)內(nèi)容~


1.Android文件的操作模式

學(xué)過Java的同學(xué)都知道,我們新建文件,然后就可以寫入數(shù)據(jù)了,但是Android卻不一樣,因?yàn)锳ndroid是 基于Linux的,我們在讀寫文件的時(shí)候,還需加上文件的操作模式,Android中的操作模式如下:

1.png


2.文件的相關(guān)操作方法

2.png

3.文件讀寫的實(shí)現(xiàn)

Android中的文件讀寫和Java中的文件I/O相同,流程也很簡單,下面我們來寫個(gè)簡單的示例:

實(shí)現(xiàn)效果圖:

3.gif

PS:這里用的是模擬器,因?yàn)楣P者的N5并沒有root,看不到文件的存儲目錄,下面我們打開DDMS 的File Exploer可以看到,在data/data/<包名>/file中有我們寫入的文件:

4.png

我們可以點(diǎn)擊右上角的響應(yīng)圖標(biāo)將文件導(dǎo)入到電腦中,并且打開驗(yàn)證寫入的內(nèi)容:

5.jpg6.png

代碼實(shí)現(xiàn):

首先是布局文件:main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jay.example.filedemo1.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nametitle" />

    <EditText
        android:id="@+id/editname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/detailtitle" />

    <EditText
        android:id="@+id/editdetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minLines="2" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnsave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btnwrite" />

        <Button
            android:id="@+id/btnclean"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/btnclean" />
    </LinearLayout>

    <Button
        android:id="@+id/btnread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnread" />

</LinearLayout>

然后我們來寫一個(gè)文件協(xié)助類:FileHelper.java

/**
 * Created by Jay on 2015/9/1 0001.
 */
public class FileHelper {

    private Context mContext;

    public FileHelper() {
    }

    public FileHelper(Context mContext) {
        super();
        this.mContext = mContext;
    }

    /*
    * 這里定義的是一個(gè)文件保存的方法,寫入到文件中,所以是輸出流
    * */
    public void save(String filename, String filecontent) throws Exception {
        //這里我們使用私有模式,創(chuàng)建出來的文件只能被本應(yīng)用訪問,還會覆蓋原文件哦
        FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE);
        output.write(filecontent.getBytes());  //將String字符串以字節(jié)流的形式寫入到輸出流中
        output.close();         //關(guān)閉輸出流
    }


    /*
    * 這里定義的是文件讀取的方法
    * */
    public String read(String filename) throws IOException {
        //打開文件輸入流
        FileInputStream input = mContext.openFileInput(filename);
        byte[] temp = new byte[1024];
        StringBuilder sb = new StringBuilder("");
        int len = 0;
        //讀取文件內(nèi)容:
        while ((len = input.read(temp)) > 0) {
            sb.append(new String(temp, 0, len));
        }
        //關(guān)閉輸入流
        input.close();
        return sb.toString();
    }

}

最后是MainActivity.java,我們在這里完成相關(guān)操作:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editname;
    private EditText editdetail;
    private Button btnsave;
    private Button btnclean;
    private Button btnread;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();
        bindViews();
    }


    private void bindViews() {
        editdetail = (EditText) findViewById(R.id.editdetail);
        editname = (EditText) findViewById(R.id.editname);
        btnclean = (Button) findViewById(R.id.btnclean);
        btnsave = (Button) findViewById(R.id.btnsave);
        btnread = (Button) findViewById(R.id.btnread);

        btnclean.setOnClickListener(this);
        btnsave.setOnClickListener(this);
        btnread.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnclean:
                editdetail.setText("");
                editname.setText("");
                break;
            case R.id.btnsave:
                FileHelper fHelper = new FileHelper(mContext);
                String filename = editname.getText().toString();
                String filedetail = editdetail.getText().toString();
                try {
                    fHelper.save(filename, filedetail);
                    Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入成功", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入失敗", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btnread:
                String detail = "";
                FileHelper fHelper2 = new FileHelper(getApplicationContext());
                try {
                    String fname = editname.getText().toString();
                    detail = fHelper2.read(fname);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

4.讀取SD卡上的文件

讀取流程圖

7.png

代碼示例

運(yùn)行效果圖

8.gif

同樣打開DDMS的File Explorer,在舊版本的系統(tǒng)上我們可以直接在mmt\sdcard上找到,但是新版本 的就可能需要我們自己找找了,首先我們來到這個(gè)路徑下:

9.png

點(diǎn)開sdcard,但是沒東西,我們繼續(xù)找嘮叨后面這個(gè)/storage/emulated/legacy下找:

10.png

好吧,他又跳到別的地方去了,我們繼續(xù)找/storage/shell/emilated/0

11.png

果然找到了,我們在SD卡里生成的test.txt!導(dǎo)出到電腦看下里面的內(nèi)容:

12.png

嘿嘿,果然讀寫SD卡成功~接下來我們來看下代碼是怎么寫的:

代碼實(shí)現(xiàn):

main_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jay.example.filedemo2.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清輸入文件名" />

    <EditText
        android:id="@+id/edittitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="文件名" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清輸入文件內(nèi)容" />

    <EditText
        android:id="@+id/editdetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="文件內(nèi)容" />

    <Button
        android:id="@+id/btnsave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存到SD卡" />

    <Button
        android:id="@+id/btnclean"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="清空" />

    <Button
        android:id="@+id/btnread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="讀取sd卡中的文件" />

</LinearLayout>

接著我們來寫一個(gè)SD操作類:SDFileHelper.java

/**
 * Created by Jay on 2015/9/1 0001.
 */
public class SDFileHelper {

    private Context context;

    public SDFileHelper() {
    }

    public SDFileHelper(Context context) {
        super();
        this.context = context;
    }

    //往SD卡寫入文件的方法
    public void savaFileToSD(String filename, String filecontent) throws Exception {
        //如果手機(jī)已插入sd卡,且app具有讀寫sd卡的權(quán)限
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //這里就不要用openFileOutput了,那個(gè)是往手機(jī)內(nèi)存中寫數(shù)據(jù)的
            FileOutputStream output = new FileOutputStream(filename);
            output.write(filecontent.getBytes());
            //將String字符串以字節(jié)流的形式寫入到輸出流中
            output.close();
            //關(guān)閉輸出流
        } else Toast.makeText(context, "SD卡不存在或者不可讀寫", Toast.LENGTH_SHORT).show();
    }

    //讀取SD卡中文件的方法
    //定義讀取文件的方法:
    public String readFromSD(String filename) throws IOException {
        StringBuilder sb = new StringBuilder("");
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename;
            //打開文件輸入流
            FileInputStream input = new FileInputStream(filename);
            byte[] temp = new byte[1024];

            int len = 0;
            //讀取文件內(nèi)容:
            while ((len = input.read(temp)) > 0) {
                sb.append(new String(temp, 0, len));
            }
            //關(guān)閉輸入流
            input.close();
        }
        return sb.toString();
    }


}

接著MainActivity.java實(shí)現(xiàn)相關(guān)邏輯:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText editname;
    private EditText editdetail;
    private Button btnsave;
    private Button btnclean;
    private Button btnread;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = getApplicationContext();
        bindViews();
    }

    private void bindViews() {
        editname = (EditText) findViewById(R.id.edittitle);
        editdetail = (EditText) findViewById(R.id.editdetail);
        btnsave = (Button) findViewById(R.id.btnsave);
        btnclean = (Button) findViewById(R.id.btnclean);
        btnread = (Button) findViewById(R.id.btnread);

        btnsave.setOnClickListener(this);
        btnclean.setOnClickListener(this);
        btnread.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnclean:
                editdetail.setText("");
                editname.setText("");
                break;
            case R.id.btnsave:
                String filename = editname.getText().toString();
                String filedetail = editdetail.getText().toString();
                SDFileHelper sdHelper = new SDFileHelper(mContext);
                try
                {
                    sdHelper.savaFileToSD(filename, filedetail);
                    Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入成功", Toast.LENGTH_SHORT).show();
                }
                catch(Exception e){
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "數(shù)據(jù)寫入失敗", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btnread:
                String detail = "";
                SDFileHelper sdHelper2 = new SDFileHelper(mContext);
                try
                {
                    String filename2 = editname.getText().toString();
                    detail = sdHelper2.readFromSD(filename2);
                }
                catch(IOException e){e.printStackTrace();}
                Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

最后別忘記在AndroidManifest.xml寫上讀寫SD卡的權(quán)限哦!

<!-- 在SDCard中創(chuàng)建與刪除文件權(quán)限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard寫入數(shù)據(jù)權(quán)限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

5.關(guān)于原生模擬器SD卡的問題

如果是真機(jī)調(diào)試的話通常都是可以的,對于原生虛擬機(jī)的話就問題多多了,再我們前面使用 Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)可能 一直返回的是false,就是SD卡不存在,這個(gè)是主要的問題,現(xiàn)在新版本的SDK都會在 創(chuàng)建AVD的時(shí)候會同時(shí)申請一塊SD卡的存儲區(qū)域的

13.jpg

對于舊版本的sdk或者其他原因可能需要手動(dòng)關(guān)聯(lián)下sd卡,設(shè)置如下:
①找到創(chuàng)建好的avd的鏡像的路徑:
點(diǎn)擊打開avd界面,點(diǎn)擊detail,查看avd鏡像的目錄下

14.jpg

②來到avd鏡像所在的路徑下,復(fù)制sdcard.img的路徑:

15.jpg

比如我的:-sdcard C:\Users\Administrator.android\avd\Jay4.2.avd\sdcard.img

③接著點(diǎn)擊 來到以下界面:

16.jpg

最后apply以下,然后Run就可以了!


6.讀取raw和assets文件夾下的文件

相信大家對兩個(gè)文件夾并不陌生,如果我們不想自己的文件被編譯成二進(jìn)制文件的話, 我們可以把文件放到這兩個(gè)目錄下,而兩者的區(qū)別如下:

  • res/raw:文件會被映射到R.java文件中,訪問的時(shí)候直接通過資源ID即可訪問,而且 他不能有目錄結(jié)構(gòu),就是不能再創(chuàng)建文件夾
  • assets:不會映射到R.java文件中,通過AssetManager來訪問,能有目錄結(jié)構(gòu),即, 可以自行創(chuàng)建文件夾

讀取文件資源:

res/raw

InputStream is =getResources().openRawResource(R.raw.filename);

assets

AssetManager am =  getAssets();  
InputStream is = am.open("filename");

代碼下載:


本節(jié)小結(jié):

好的,關(guān)于Android的數(shù)據(jù)存儲與訪問的第一節(jié)——文件讀寫就到這里,如果在學(xué)習(xí)本文中 遇到什么問題,或者覺得有些紕漏的地方,歡迎提出,萬分感激,謝謝~