Android读写操作之内存的读写操作

来源:互联网 发布:november rain 知乎 编辑:程序博客网 时间:2024/06/05 05:38

今天我们讲下Android中文件的读写操作。其中分为两种,一种是对于Android中内存的读写操作,

还有一种是Android中sdcard的读写操作。

下面我们先介绍一下Android中内存的读写操作。我们直接看代码吧,都有很详细的注释:

package com.example.file_01;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import android.os.Bundle;import android.os.FileObserver;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{private Button read, write,delete,show;private EditText et1,et2;//声明一个要写入的文件名,默认存储路径为data文件夹下final String FILE_NAME = "myFile.txt";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.file_layout);        et1 = (EditText) findViewById(R.id.et1);        et2 = (EditText) findViewById(R.id.et2);        read = (Button)findViewById(R.id.btn1);        write = (Button)findViewById(R.id.btn2);        delete = (Button)findViewById(R.id.btn3);        show = (Button)findViewById(R.id.btn4);        read.setOnClickListener(this);        write.setOnClickListener(this);        delete.setOnClickListener(this);        show.setOnClickListener(this);    }@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn1:try {//读取文件通过openFileInputBufferedReader bufr = new BufferedReader(new InputStreamReader(openFileInput(FILE_NAME),"utf-8"));String line = "";String result = "";while((line = bufr.readLine()) != null){result += line;}//把读取的内容显示到Activity中et2.setText(result);bufr.close();Toast.makeText(MainActivity.this, "读取文件成功", 0).show();} catch (UnsupportedEncodingException e1) {e1.printStackTrace();} catch (FileNotFoundException e1) {//如果文件不存在,在FileNotFoundException中说明Toast.makeText(MainActivity.this, "文件不存在", 0).show();e1.printStackTrace();} catch (IOException e) {e.printStackTrace();}break;case R.id.btn2:try {//写入文件通过openFileOutputFileOutputStream fos = openFileOutput(FILE_NAME, MODE_PRIVATE);fos.write(et1.getText().toString().getBytes());fos.close();Toast.makeText(MainActivity.this, "写入文件成功", 0).show();et1.setText("");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}break;case R.id.btn3://删除文件deleteFile(FILE_NAME);Toast.makeText(MainActivity.this, "删除文件成功", 0).show();break;case R.id.btn4://列出文件中内容的名字String[] file = fileList();for (int i = 0; i < file.length; i++) {Log.i("main", file[i]);}break;default:break;}}    }
xml文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:padding="20dp">    <EditText         android:id="@+id/et1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入内容"/>    <EditText         android:id="@+id/et2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:editable="false"/>    <Button         android:id="@+id/btn1"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_marginTop="10dp"        android:text="读取文件"/>    <Button         android:id="@+id/btn2"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_marginTop="10dp"        android:text="写入文件"/>    <Button         android:id="@+id/btn3"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_marginTop="10dp"        android:text="删除文件"/>    <Button         android:id="@+id/btn4"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_marginTop="10dp"        android:text="查看文件"/></LinearLayout>

0 0
原创粉丝点击