程序界面:
布局文件:
<?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"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/age"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SavaDisk"
android:id="@+id/saveDisk"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ReadDisk"
android:id="@+id/readDisk"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SavaSD"
android:id="@+id/saveSD"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ReadSD"
android:id="@+id/readSD"
/>
</LinearLayout>
<EditText
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/etContent"
/>
</LinearLayout>
SD卡和磁盘的存入和读取
SD卡位置:
运行一个模拟器后,在File Explorer下面的如下目录中:
![](http://hi.csdn.net/attachment/201110/1/0_13174854888ep0.gif)
如我的放到SD卡中的mytest.txt文件
APP磁盘位置:
在data/data/包名/files/你的文件
在操作之前,先要在Mani fest中加入权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS">
</uses-permission>
<!-- SD卡可写 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
MainActivity中的代码:
package cn.learn.sdcard;
import java.io.File;
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.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SDCardActivity extends Activity implements OnClickListener{
private EditText etAge;
private EditText etName;
private EditText etContent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到
Button btnSaveToDisk = (Button)findViewById(R.id.saveDisk);
Button btnReadFromDisk = (Button)findViewById(R.id.readDisk);
Button btnSaveSD = (Button)findViewById(R.id.saveSD);
Button btnReadSD = (Button)findViewById(R.id.readSD);
etContent = (EditText)findViewById(R.id.etContent);
etName = (EditText)findViewById(R.id.name);
etAge = (EditText)findViewById(R.id.age);
//设置响应事件
btnSaveToDisk.setOnClickListener(this);
btnReadFromDisk.setOnClickListener(this);
btnReadSD.setOnClickListener(this);
btnSaveSD.setOnClickListener(this);
}
//事件响应
public void onClick(View v) {
int id = v.getId();
String name = etName.getText().toString();
String age = etAge.getText().toString();
String str = "name:"+name+"age:"+age;//得到要写入的字串
switch (id) {
case R.id.saveDisk://与入磁盘
//下面的模式 有四种
//Context.MODE_APPEND 追加
//Context.MODE_PRIVATE 每次都重写文件
//Context.MODE_WORLD_READABLE 全局可读
//Context.MODE_WORLD_WRITEABLE 全局可写
try {
FileOutputStream fos = this.openFileOutput("mytest.txt", Context.MODE_APPEND);//打开一个fos流
fos.write(str.getBytes());
fos.close();
Toast.makeText(this, "写入完成", 1).show();
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.readDisk:
try {
FileInputStream fis = this.openFileInput("mytest.txt");//直接读就OK
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
etContent.setText(new String(bytes));//设置显示
} catch (Exception e) {
e.printStackTrace();
}
break;
case R.id.saveSD://与入SD
try {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {// 如果已经挂载好SD卡
File dir = Environment.getExternalStorageDirectory();
File newFile = new File(dir, "mytest.txt");
FileOutputStream fos = new FileOutputStream(newFile,true);
try {
fos.write(str.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "挂载问题", 1).show();
return;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "文件未找到", 1).show();
return;
}
break;
case R.id.readSD:
try {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {// 如果已经挂载好SD卡
File dir = Environment.getExternalStorageDirectory();
File newFile = new File(dir, "mytest.txt");
FileInputStream fis = new FileInputStream(newFile);
try {
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
etContent.setText(new String(bytes));
Toast.makeText(this, "读取完成", 1).show();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "挂载问题", 1).show();
return;
}
} catch (FileNotFoundException e) {
Toast.makeText(this, "文件未找到", 1).show();
return;
}
break;
default:
break;
}
}
}