java文件
package edu.hrbeu.InternalFileDemo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public class InternalFileDemoActivity extends Activity {
private final String FILE_NAME = "fileDemo.txt";
private TextView labelView;
private TextView displayView;
private CheckBox appendBox ;
private EditText entryText;
private EditText edit1;
private EditText edit2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
labelView = (TextView)findViewById(R.id.label);
displayView = (TextView)findViewById(R.id.display);
appendBox = (CheckBox)findViewById(R.id.append);
entryText = (EditText)findViewById(R.id.entry);
edit1=(EditText)findViewById(R.id.editText1);
edit2=(EditText)findViewById(R.id.editText2);
Button writeButton = (Button)findViewById(R.id.write);
Button readButton = (Button)findViewById(R.id.read);
writeButton.setOnClickListener(writeButtonListener);
readButton.setOnClickListener(readButtonListener);
entryText.selectAll();
entryText.findFocus();
}
OnClickListener writeButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
FileOutputStream fos = null;
try {
if (appendBox.isChecked()){
fos = openFileOutput(FILE_NAME,Context.MODE_APPEND);
}
else {
fos = openFileOutput(FILE_NAME,Context.MODE_PRIVATE);
}
String text = entryText.getText().toString()+" "+edit1.getText().toString()+" "+edit2.getText().toString();
fos.write(text.getBytes());
//labelView.setText("文件写入成功,写入长度:"+text.length());
entryText.setText("");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
if (fos != null){
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
};
OnClickListener readButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
displayView.setText("");
FileInputStream fis = null;
try {
fis = openFileInput(FILE_NAME);
if (fis.available() == 0){
return;
}
byte[] readBytes = new byte[fis.available()];
while(fis.read(readBytes) != -1){
}
String text = new String(readBytes);
displayView.setText(text);
//labelView.setText("文件读取成功,文件长度:"+text.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
};
}
布局文件 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/label"
android:layout_width="74dp"
android:layout_height="wrap_content"
android:text="姓名" />
<EditText
android:id="@+id/entry"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:ems="10"
android:text="输入文件内容" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号" />
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText1"
android:layout_width="178dp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级" />
<EditText
android:id="@+id/editText2"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取文件" />
<Button
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写入文件" />
<CheckBox android:id="@+id/append"
android:text="追加模式"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<TextView android:id="@+id/display"
android:text="文件内容显示区域"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:textColor="#000000" >
</TextView>
</LinearLayout>