最近在学习安卓开发,有用到读写文件的操作,从网上找了好几个例子,终于成功运行了,分享给大家
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/ed1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/ed2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/write"
android:layout_below="@+id/write"
android:layout_marginTop="43dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/write"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ed1"
android:layout_below="@+id/ed1"
android:layout_marginTop="30dp"
android:text="@string/write" />
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/read"
android:layout_alignParentBottom="true"
android:layout_marginBottom="22dp"
android:text="@string/delete" />
<Button
android:id="@+id/read"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ed2"
android:layout_below="@+id/ed2"
android:layout_marginTop="24dp"
android:text="@string/read" />
</RelativeLayout>
package com.example.test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button read;
private Button write;
private EditText ed1;
private EditText ed2;
private EditText ed3;
private Button delete;
private Spinner spiEdu=null;
private ArrayAdapter<CharSequence> adapteEdu=null;
private List<CharSequence> dataEdu=null;//定义一个集合数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
read = (Button) findViewById(R.id.read);
write = (Button) findViewById(R.id.write);
delete = (Button) findViewById(R.id.delete);
ed2 = (EditText) findViewById(R.id.ed2);
ed1 = (EditText) findViewById(R.id.ed1);
write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = ed1.getText().toString();
if (!str.equals("")) {
write(str);
}
}
});
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
read();
}
});
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
deleteFiles("test.txt");
// String str = ed3.getText().toString();
// if (!str.equals("")) {
// deleteFiles("test.txt");
// } else {
// ed3.setText(str + ":该文件输入错误或不存在!");
// }
}
});
}
private void write(String content) {
try {
// 以追加的方式打开文件输出流
//this.MODE_APPEND追加
//this.MODE_PRIVATE覆盖
FileOutputStream fileOut = this.openFileOutput("test.txt",
this.MODE_APPEND);
// 写入数据
fileOut.write(content.getBytes());
// 关闭文件输出流
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void read() {
try {
ed2.setText("");
// 打开文件输入流
FileInputStream fileInput = this.openFileInput("test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(
fileInput));
String str = null;
StringBuilder stb = new StringBuilder();
while ((str = br.readLine()) != null) {
stb.append(str);
}
ed2.setText(stb);
/*this.dataEdu=new ArrayList<CharSequence>();
//String tt=ed2.getText().toString();
List list=getList(stb.toString(),"仓房");
//String[] tt2=stb.toString().split("");
for(int i=0;i<list.size();i++){
this.dataEdu.add(list.get(i).toString());
}
// this.dataEdu.add("大学");
// this.dataEdu.add("研究生");
// this.dataEdu.add("高中");
this.spiEdu=(Spinner)super.findViewById(R.id.seledu);
this.spiEdu.setPrompt("请选择您的仓位");
this.adapteEdu=new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item,this.dataEdu);
this.adapteEdu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spiEdu.setAdapter(this.adapteEdu);*/
} catch (Exception e) {
e.printStackTrace();
}
}
/* public List getList(String str,String type){
List list=new ArrayList();
//String str="仓房:1号仓,2号仓,3号仓,4号仓;货位:1号货位,2号货位,3号货位,4号货位";
String[] strs=str.split(";");
for(String s:strs){
if(s.substring(0, s.indexOf(":")).equals(type)){
String tt=s.substring(s.indexOf(":")+1,s.length());
String[] tts=tt.split(",");
for(String t:tts){
// System.out.println(t);
list.add(t);
}
}
}
return list;
}*/
// 删除指定的文件
private void deleteFiles(String fileName) {
try {
// 获取data文件中的所有文件列表
List<String> name = Arrays.asList(this.fileList());
// this.deleteFiles(fileName);
if (name.contains(fileName)) {
this.deleteFile(fileName);
showMessage(fileName + ":该文件成功删除!");
} else
showMessage(fileName + ":该文件输入错误或不存在!");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// 显示信息
public void showMessage(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
其中有个方法是我用来读取文件内容,生成下拉列表的,部分代码没有删除,大家在运行的时候需要注意!