android文件操作OpenFileInput OpenFileOutput

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文件存储在了 datadata报名files目录下

原文地址:https://www.cnblogs.com/alwaysfirst/p/4009164.html