一、运行效果图
二、核心代码
1.MainActivity的代码:
public class MainActivity extends Activity implements OnClickListener{
private Button read_btn;
private Button write_btn;
private TextView show;
private EditText editText;
private String FILE_NAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
read_btn = (Button)findViewById(R.id.read);
write_btn = (Button)findViewById(R.id.write);
show = (TextView)findViewById(R.id.show);
editText = (EditText)findViewById(R.id.editText);
read_btn.setOnClickListener(this);
write_btn.setOnClickListener(this);
}
public void onClick(View v){
switch(v.getId()){
case R.id.read:
String str=read(FILE_NAME);
if(str!=null){
show.setText(str);
}else{
show.setText("未读取到内容");
}
break;
case R.id.write:
String content=editText.getText().toString();
write(content,FILE_NAME);
break;
default:
break;
}
}
/**
* 写入方法
* @param content 写入的内容
* @param fileName 写入文件的文件名称
*/
private void write(String content,String fileName){
try{
//手机是否插入了SD卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取SD卡的目录
File sdCardDir=Environment.getExternalStorageDirectory();
File file=new File(sdCardDir.getCanonicalPath()+"/"+fileName);
//创建RandomAccessFile对象
RandomAccessFile raf=new RandomAccessFile(file,"rw");
//将文件记录指针移动到最后
raf.seek(file.length());
//写入文件
raf.write(content.getBytes());
raf.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 读取文件
* @param fileName 文件名称
* @return 读取的字符串
*/
private String read(String fileName){
try{
//手机是否插入了SD卡
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//获取SD卡的目录
File sdCardDir=Environment.getExternalStorageDirectory();
FileInputStream fis=new FileInputStream(sdCardDir.getCanonicalPath()+"/"+fileName);
BufferedReader bf=new BufferedReader(new InputStreamReader(fis));
StringBuffer sb=new StringBuffer("");
String line=null;
while((line=bf.readLine())!=null){
sb.append(line);
}
bf.close();
return sb.toString();
}
return null;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
@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;
}
}
2.activity_main的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
<Button
android:id="@+id/read"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:text="读取文件"/>
<Button
android:id="@+id/write"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/read"
android:text="写入文件"/>
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/write"
android:text=""/>
</RelativeLayout>
三、遇到的问题
在安卓自带的模拟器 上无法写入数据,没有SD卡,只有新下载的模拟器才能运行出效果