android 读写文件示例
时间:2014-05-07 03:15:13
收藏:0
阅读:464
(1)程序说明
代码中的两个按钮处理事件,分别进行了文本的读写操作。
1)文件写操作
首先调用Activity的openFileOutPut()方法获得文本文件的输出流,第一个参数为文本文件的名字,第二个为文件的打开方式
接着调用Outputstream对象的write()方法将Textview中获得文本信息写入outputstream对象,最后调用close()方法完成写入操作。
2)文件读操作
首先调用Activity的openFileInPut()方法获得文本文件的输入流,
接着调用输入流对象的read()方法将输入流中的字节信息读入一个ByteArrayOutputStream对象,最后将ByteArrayOutputStream转换为string显示在Textview中
(2)布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/label_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:textSize="20dp" /> <EditText android:id="@+id/filename" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_alignTop="@id/label_01" android:layout_toRightOf="@id/label_01" /> </RelativeLayout> <TextView android:id="@+id/label_02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/neirong" /> <EditText android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="120px" android:ems="10" > <requestFocus /> </EditText> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/savebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="@string/save" /> <Button android:id="@+id/readbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="right" android:text="@string/read" /> </LinearLayout> <TextView android:id="@+id/textcontent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.10" /> </LinearLayout>
(2)代码
package com.liuzuyi.file; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.os.Build; public class MainActivity extends Activity { private EditText filename; private EditText context; private TextView textcontent; private static final String TAG="simplefile"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); filename=(EditText)findViewById(R.id.filename); context=(EditText)findViewById(R.id.content); textcontent=(TextView)findViewById(R.id.textcontent); Button savebtn=(Button)this.findViewById(R.id.savebutton); Button viewbtn=(Button)this.findViewById(R.id.readbutton); savebtn.setOnClickListener(l); viewbtn.setOnClickListener(l); } private View.OnClickListener l =new OnClickListener() { public void onClick(View v) { Button button =(Button)v; String namestr = filename.getText().toString().trim(); String contentstr =context.getText().toString(); switch ( button.getId() ) { case R.id.savebutton: String resid_s ="success"; OutputStream filsos= null; try { filsos=MainActivity.this.openFileOutput(namestr+".txt", Context.MODE_APPEND) ; filsos.write(contentstr.getBytes()); filsos.close(); } catch (Exception e) { resid_s = "faile"; e.printStackTrace(); } Toast.makeText(MainActivity.this, resid_s,Toast.LENGTH_LONG).show(); Log.i(TAG, namestr); Log.i(TAG, contentstr); break; case R.id.readbutton: String resid_v ="success"; InputStream filsIs= null; String contentst = null; try { filsIs=MainActivity.this.openFileInput(namestr+".txt") ; ByteArrayOutputStream oStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while( (len = filsIs.read(buffer)) != -1 ) {oStream.write(buffer,0,len);} contentst=oStream.toString(); oStream.close(); filsIs.close(); } catch (Exception e) { resid_v ="faile"; e.printStackTrace(); } textcontent.setText( contentst); Log.i(TAG, contentst); Toast.makeText(MainActivity.this, resid_v,Toast.LENGTH_LONG).show(); Log.i(TAG,namestr); break; } } }; }
评论(0)