可以把数据保存在SD卡中,在SD卡中建立一个文件去保存数据,这里说一下 ,SD卡 用户是可以访问的,也就是说可以把一些可有可无的数据存在SD卡中,即使用户删除了卡中的内容也不会影像软件的使用。

将文件在SD卡中删除

- public class loadSDActivity extends Activity {
- public final static String FILE_NAME = "b.txt";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- setContentView(R.layout.sdfile);
- /**读取内容**/
- String content = loadFile();
- if(content == null) {
- content ="上次没有输入内容请输入";
- }
- final EditText editContent = (EditText)findViewById(R.id.sdfile_et0);
- editContent.setHint("上次输入SD卡的内容的为【 " +content + "】");
- Button button0 = (Button)findViewById(R.id.sdfile_button0);
- /**监听按钮点击后保存用户输入信息到SD卡中**/
- button0.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- /**拿到用户输入的信息**/
- String content = editContent.getText().toString();
- /**开始保存入SD卡**/
- saveFile(content);
- ShowDialog("保存SD卡文件成功");
- }
- });
- Button button1 = (Button)findViewById(R.id.sdfile_button1);
- /**去清除SD卡保存的内容**/
- button1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- cleanFile();
- ShowDialog("清除SD卡文件中的内容成功");
- }
- });
- Button button2 = (Button)findViewById(R.id.sdfile_button2);
- /**删除SD卡保存的文件**/
- button2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- DeleteSDFile();
- }
- });
- super.onCreate(savedInstanceState);
- }
- /**
- * 保存入SD卡中
- * @param str
- */
- public void saveFile(String str) {
- FileOutputStream fileOutputStream = null;
- File file = new File(Environment.getExternalStorageDirectory(),
- FILE_NAME);
- try {
- fileOutputStream = new FileOutputStream(file);
- fileOutputStream.write(str.getBytes());
- fileOutputStream.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取SD卡的内容
- * @return
- */
- public String loadFile() {
- String path = Environment.getExternalStorageDirectory() +"/" + FILE_NAME;
- try {
- FileInputStream fi = new FileInputStream(path);
- BufferedReader br = new BufferedReader(new InputStreamReader(
- fi));
- String readString = new String();
- while ((readString = br.readLine()) != null) {
- //数据多的话须要在这里处理 readString
- return readString;
- }
- fi.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 删除SD卡
- */
- public void DeleteSDFile() {
- String path = Environment.getExternalStorageDirectory() + "/"
- + FILE_NAME;
- File file1 = new File(path);
- boolean isdelte = file1.delete();
- if(isdelte) {
- ShowDialog("删除SD卡成功");
- }else {
- finish();
- }
- }
- /**
- * 因为java删除文件内容只有一种实现方法,就是把整个文件重写,只是把须要删除的那一条记录去除掉
- */
- public void cleanFile() {
- //如果只须要删除文件中的一部分内容则须要在这里对字符串做一些操作
- String cleanStr = "";
- FileOutputStream fileOutputStream = null;
- File file = new File(Environment.getExternalStorageDirectory(),
- FILE_NAME);
- try {
- fileOutputStream = new FileOutputStream(file);
- fileOutputStream.write(cleanStr.getBytes());
- fileOutputStream.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void ShowDialog(String str) {
- AlertDialog.Builder builder = new AlertDialog.Builder(loadSDActivity.this);
- builder.setIcon(R.drawable.icon);
- builder.setTitle(str);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- finish();
- }
- });
- builder.show();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <ImageView android:id="@+id/sdfile_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/g"
- android:layout_gravity="center"
- />
- <EditText android:id="@+id/sdfile_et0"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请输入需要保存到SD卡的内容">
- </EditText>
- <Button android:id="@+id/sdfile_button0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="保存输入内容到SD卡">
- </Button>
- <Button android:id="@+id/sdfile_button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="清除SD卡保存文件的内容">
- </Button>
- <Button android:id="@+id/sdfile_button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="删除SD卡中保存的文件">
- </Button>
- </LinearLayout>
最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2011年资料/Android入门教程/Android软件开发之数据的新建 储存 读取 删除/
