android檔案操作OpenFileInput OpenFileOutput

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   使用   ar   

android 中對檔案的IO操作,OpenFileInput OpenFileOutput 返回對象為Java Io 的FileInputStream和FileOutputStream

我們通過一個小例子,來看下是如何使用的

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.ssln.fileoperator.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/tvMsg" /></RelativeLayout>

mainactivity.java

package com.ssln.fileoperator;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.apache.http.util.EncodingUtils;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {    private String fileName = "text.txt";    private String message = "這是通過OpenFileOupput寫出,OpenFileInput讀取的內容";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //寫出檔案        WriteFile();        //尋找控制項        TextView tvMsg=(TextView)findViewById(R.id.tvMsg);        //讀取內容        tvMsg.setText(ReadFile());    }    /**     * 讀取檔案     *      * @return 讀取到的檔案內容     */    private String ReadFile() {        FileInputStream inputStream;        byte[] buffer = null;        try {            inputStream = this.openFileInput(fileName);            try {                // 擷取檔案內容長度                int fileLen = inputStream.available();                // 讀取內容到buffer                buffer = new byte[fileLen];                inputStream.read(buffer);            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        }        // 返迴文本資訊        if (buffer != null)            return EncodingUtils.getString(buffer, "utf-8");        else            return "";    }    /**     * 寫出檔案內容到檔案     */    private void WriteFile() {        try {            // 開啟檔案,該檔案只能由調用該方法的應用程式訪問            // MODE_PRIVATE 該檔案只能由調用該方法的應用程式訪問            // MODE_APPEND 如果檔案已存在,就在結尾追加內容,而不是覆蓋檔案            // MODE_WORLD_READABLE 賦予所有應用程式讀許可權            // MODE_WORLD_WRITEABLE 賦予所有應用程式寫入權限            FileOutputStream outStream = this.openFileOutput(fileName,                    MODE_PRIVATE);            // 將文本轉換為位元組集            byte[] data = message.getBytes();            try {                // 寫出檔案                outStream.write(data);                outStream.flush();                outStream.close();            } catch (IOException e) {                e.printStackTrace();            }        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }}

運行效果

 

text.txt檔案儲存體在了 data\data\報名\files目錄下

android檔案操作OpenFileInput OpenFileOutput

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.