1java部分
package com.example.sdcard;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
// 文件名字符串常量和目录
//public final static String DATASTORAGE_SDCard_FILE_NAME = "sdcardTest.txt";
public class MainActivity extends Activity
{
// 文件名字符串常量和目录
public final static String DATASTORAGE_SDCard_FILE_NAME = "sdcardTest.txt";
// 按钮和编辑框对象
private Button mBtn1, mBtn2, mBtn3, mBtn4;
private EditText mEt;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String content = readSDCard(DATASTORAGE_SDCard_FILE_NAME);
if (TextUtils.isEmpty(content))
{
content = "上次输入内容为空,请输入内容!";
}
mEt = (EditText) findViewById(R.id.mydatastorage_sdcard_ET01);
mEt.setHint("上次输入SD卡的内容的为:"+content );
mBtn1 = (Button) findViewById(R.id.mydatastorage_sdcard_Btn01);
mBtn2 = (Button) findViewById(R.id.mydatastorage_sdcard_Btn02);
mBtn3 = (Button) findViewById(R.id.mydatastorage_sdcard_Btn03);
mBtn4 = (Button) findViewById(R.id.mydatastorage_sdcard_Btn04);
mBtn1.setOnClickListener(new OnClickListener()// 写入
{
@Override
public void onClick(View v)
{
String content = mEt.getText().toString();
saveToSDCard(DATASTORAGE_SDCard_FILE_NAME, content);
}
});
mBtn2.setOnClickListener(new OnClickListener()// 读取
{
@Override
public void onClick(View v)
{
String content = readSDCard(DATASTORAGE_SDCard_FILE_NAME);
if(TextUtils.isEmpty(content))
showInfo("读取文件为空!");
mEt.setText(content);
}
});
mBtn3.setOnClickListener(new OnClickListener()// 清除
{
@Override
public void onClick(View v)
{
clearSDFile(DATASTORAGE_SDCard_FILE_NAME);
String content = readSDCard(DATASTORAGE_SDCard_FILE_NAME);
mEt.setText(content);
}
});
mBtn4.setOnClickListener(new OnClickListener()// 删除
{
@Override
public void onClick(View v)
{
deleteSDFile(DATASTORAGE_SDCard_FILE_NAME);
}
});
}
//
/* 文件写入(文件保存到SDCard中) */
public void saveToSDCard(String filename, String content)
{
try
{
// Log.d(LOG_TAG, "Start Write");
// ①获取扩展SD卡设备状态
String SDCardState = android.os.Environment.getExternalStorageState();
if (SDCardState.equals(android.os.Environment.MEDIA_MOUNTED)) // 判断sdcard是否存在(可读可写权限)
{
// ②获取扩展存储设备的文件目录
File SDFile = android.os.Environment.getExternalStorageDirectory();
// ③打开文件
// File file = new File(SDFile,filename);
File file = new File(SDFile.getAbsolutePath() + File.separator + filename);
// 判断是否存在,不存在则创建
if (!file.exists())
{
file.createNewFile();
}
// ④写数据
FileOutputStream fos = new FileOutputStream(file); // 获取输出流
fos.write(content.getBytes());
fos.close();
showInfo("文件写入成功!");
} else
{
showInfo("文件写入失败!");
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
//
/* 文件读取(文件加载) */
public String readSDCard(String filename)
{
try
{
String SDCardState = android.os.Environment.getExternalStorageState();
if (SDCardState.equals(android.os.Environment.MEDIA_MOUNTED)) // 只读权限
{
// 获取扩展存储设备的文件目录
File SDFile = android.os.Environment.getExternalStorageDirectory();
// 创建一个文件
File file = new File(SDFile.getAbsolutePath() + File.separator + filename);
// 判断文件是否存在
if (file.exists())
{
// 读数据
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];// 定义缓存数组
// byte[] buffer = new byte[fis.available()];
fis.read(buffer);// 读到缓存区
fis.close();
showInfo("文件读取成功!");
return new String(buffer);
}
else
{
showInfo("文件不存在,读取失败!");
return null;
}
}
else
{
showInfo("无操作权限,文件读取失败!");
return null;
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}
//
//
/* 清除记录 */
public void clearSDFile(String filename)
{
String cleanStr = "";// 如果只须要删除文件中的一部分内容则须要在这里对字符串做一些操作
FileOutputStream fos = null;
File file = new File(Environment.getExternalStorageDirectory(),
filename);
try
{
fos = new FileOutputStream(file);
fos.write(cleanStr.getBytes());
fos.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
//
/* 文件删除 */
public void deleteSDFile(String filename)
{
String path = Environment.getExternalStorageDirectory() + "/"
+ filename;
File file = new File(path);
boolean isDelte = file.delete();
if (isDelte)
{
showInfo("删除SD卡成功");
} else
{
finish();
}
}
//
/* 显示子函数 */
private void showInfo(String str)
{
// new AlertDialog.Builder(sdcard.this).setMessage(str).show();
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
//
}
2xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SDCard Test"
android:textSize="25dp" />
<EditText
android:id="@+id/mydatastorage_sdcard_ET01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="200px">
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/mydatastorage_sdcard_Btn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write" >
</Button>
<Button
android:id="@+id/mydatastorage_sdcard_Btn02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read" >
</Button>
<Button
android:id="@+id/mydatastorage_sdcard_Btn03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear" >
</Button>
<Button
android:id="@+id/mydatastorage_sdcard_Btn04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" >
</Button>
</LinearLayout>
</LinearLayout>
注:此文章参考网络资料