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
- Android读写操作之内存的读写操作
- Android数据存储之内存读写的两大内存
- android之内存操作
- Android数据访问存储之内存读写
- Android数据访问存储之内存读写
- android文件的读写操作
- Android文件的读写操作
- Android读写操作之sdcard的读写操作
- Android文件读写操作
- android读写文件操作
- android SDCARD 读写操作
- Android 文件读写操作
- android文件读写操作
- Android文件读写操作
- Android文件读写操作
- Android - 文件读写操作
- Android读写文件操作
- Android 读写文件操作
- iOS开发 ----- Controller之间的传值
- Java教程开山篇—开发环境搭建
- most of 1-1000
- nginx学习
- PHP无限分类的原理
- Android读写操作之内存的读写操作
- google adsense初次申请后广告无法显示问题
- LightOJ 1030 Discovering Gold 概率
- iOS通过UIAlertView实现Android Toast效果
- hdu 2452 Navy maneuvers-记忆化搜索
- css中的优先级
- Storyboard的简单使用
- zoj 1081 Points Within
- hdu 2389 Rain on your Parade【最大匹配】