你好,游客 登录 注册 搜索
背景:
阅读新闻

Android软件开发之数据的新建 储存 读取 删除

[日期:2011-08-12] 来源:Linux公社  作者:雨松MOMO [字体:大 中 小]
3.在SD卡中处理新建 写入 读取 删除 的操作

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






将文件在SD卡中删除


 
  1. public class loadSDActivity extends Activity {  
  2.     public final static String FILE_NAME = "b.txt";  
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.     setContentView(R.layout.sdfile);  
  6.     /**读取内容**/  
  7.     String content = loadFile();  
  8.     if(content == null) {  
  9.         content ="上次没有输入内容请输入";   
  10.     }  
  11.       
  12.     final EditText editContent = (EditText)findViewById(R.id.sdfile_et0);  
  13.     editContent.setHint("上次输入SD卡的内容的为【 " +content + "】");  
  14.     Button button0 = (Button)findViewById(R.id.sdfile_button0);  
  15.       
  16.     /**监听按钮点击后保存用户输入信息到SD卡中**/  
  17.     button0.setOnClickListener(new  OnClickListener() {  
  18.           
  19.         @Override  
  20.         public void onClick(View arg0) {  
  21.         /**拿到用户输入的信息**/  
  22.         String content = editContent.getText().toString();  
  23.         /**开始保存入SD卡**/  
  24.         saveFile(content);  
  25.         ShowDialog("保存SD卡文件成功");  
  26.         }  
  27.     });  
  28.     Button button1 = (Button)findViewById(R.id.sdfile_button1);  
  29.       
  30.     /**去清除SD卡保存的内容**/  
  31.     button1.setOnClickListener(new  OnClickListener() {  
  32.         @Override  
  33.         public void onClick(View arg0) {  
  34.         cleanFile();  
  35.         ShowDialog("清除SD卡文件中的内容成功");  
  36.         }  
  37.     });   
  38.     Button button2 = (Button)findViewById(R.id.sdfile_button2);  
  39.       
  40.     /**删除SD卡保存的文件**/  
  41.     button2.setOnClickListener(new  OnClickListener() {  
  42.         @Override  
  43.         public void onClick(View arg0) {  
  44.         DeleteSDFile();  
  45.         }  
  46.     });  
  47.       
  48.     super.onCreate(savedInstanceState);  
  49.     }  
  50.       
  51.     /** 
  52.      * 保存入SD卡中 
  53.      * @param str 
  54.      */  
  55.     public void saveFile(String str) {  
  56.     FileOutputStream fileOutputStream = null;  
  57.   
  58.     File file = new File(Environment.getExternalStorageDirectory(),  
  59.         FILE_NAME);  
  60.     try {  
  61.         fileOutputStream = new FileOutputStream(file);  
  62.         fileOutputStream.write(str.getBytes());  
  63.         fileOutputStream.close();  
  64.     } catch (FileNotFoundException e) {  
  65.         e.printStackTrace();  
  66.     }catch (IOException e) {  
  67.         e.printStackTrace();  
  68.     }  
  69.     }  
  70.      
  71.       
  72.     /** 
  73.      * 读取SD卡的内容 
  74.      * @return 
  75.      */  
  76.     public String loadFile() {  
  77.     String path = Environment.getExternalStorageDirectory() +"/" + FILE_NAME;  
  78.     try {  
  79.   
  80.         FileInputStream fi = new FileInputStream(path);  
  81.         BufferedReader br = new BufferedReader(new InputStreamReader(  
  82.             fi));  
  83.         String readString = new String();  
  84.         while ((readString = br.readLine()) != null) {  
  85.         //数据多的话须要在这里处理 readString   
  86.         return readString;  
  87.         }  
  88.         fi.close();  
  89.     } catch (FileNotFoundException e) {  
  90.         e.printStackTrace();  
  91.     } catch (IOException e) {  
  92.         e.printStackTrace();  
  93.     }  
  94.   
  95.     return null;  
  96.     }  
  97.       
  98.     /** 
  99.      * 删除SD卡 
  100.      */  
  101.     public void DeleteSDFile() {  
  102.     String path = Environment.getExternalStorageDirectory() + "/"  
  103.         + FILE_NAME;  
  104.     File file1 = new File(path);  
  105.     boolean isdelte = file1.delete();  
  106.     if(isdelte) {  
  107.         ShowDialog("删除SD卡成功");  
  108.     }else {  
  109.         finish();  
  110.     }  
  111.     }  
  112.       
  113.     /** 
  114.      * 因为java删除文件内容只有一种实现方法,就是把整个文件重写,只是把须要删除的那一条记录去除掉  
  115.      */  
  116.     public void cleanFile() {  
  117.     //如果只须要删除文件中的一部分内容则须要在这里对字符串做一些操作   
  118.     String cleanStr = "";  
  119.     FileOutputStream fileOutputStream = null;  
  120.   
  121.     File file = new File(Environment.getExternalStorageDirectory(),  
  122.         FILE_NAME);  
  123.     try {  
  124.         fileOutputStream = new FileOutputStream(file);  
  125.         fileOutputStream.write(cleanStr.getBytes());  
  126.         fileOutputStream.close();  
  127.     } catch (FileNotFoundException e) {  
  128.         e.printStackTrace();  
  129.     }catch (IOException e) {  
  130.         e.printStackTrace();  
  131.     }  
  132.     }  
  133.     public void ShowDialog(String str) {  
  134.     AlertDialog.Builder builder = new AlertDialog.Builder(loadSDActivity.this);  
  135.     builder.setIcon(R.drawable.icon);  
  136.     builder.setTitle(str);  
  137.     builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  138.         public void onClick(DialogInterface dialog, int whichButton) {  
  139.         finish();  
  140.         }  
  141.     });  
  142.     builder.show();  
  143.     }  
  144. }  

     
  1.   <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     >  
  7.     <ImageView android:id="@+id/sdfile_image"  
  8.         android:layout_width="wrap_content"  
  9.          android:layout_height="wrap_content"  
  10.         android:src="@drawable/g"  
  11.         android:layout_gravity="center"  
  12.         />  
  13.     <EditText android:id="@+id/sdfile_et0"  
  14.               android:layout_width="fill_parent"  
  15.               android:layout_height="wrap_content"  
  16.               android:hint="请输入需要保存到SD卡的内容">  
  17.     </EditText>  
  18.     <Button   android:id="@+id/sdfile_button0"  
  19.               android:layout_width="wrap_content"  
  20.               android:layout_height="wrap_content"  
  21.               android:text="保存输入内容到SD卡">  
  22.     </Button>  
  23.     <Button   android:id="@+id/sdfile_button1"  
  24.               android:layout_width="wrap_content"  
  25.               android:layout_height="wrap_content"  
  26.               android:text="清除SD卡保存文件的内容">  
  27.     </Button>  
  28.     <Button   android:id="@+id/sdfile_button2"  
  29.               android:layout_width="wrap_content"  
  30.               android:layout_height="wrap_content"  
  31.               android:text="删除SD卡中保存的文件">  
  32.     </Button>  
  33. </LinearLayout>  

 

最后如果你还是觉得我写的不够详细 看的不够爽 不要紧我把源代码的下载地址贴出来

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2011年资料/Android入门教程/Android软件开发之数据的新建 储存 读取 删除/

linux
相关资讯       Android教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款