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

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

[日期:2011-08-12] 来源:Linux公社  作者:雨松MOMO [字体:大 中 小]
2.在本地data文件下使用自己生成的文件处理数据的 新建 储存 读取 删除

       如果说不想把内容存在SharedPreferences中的话,我们可以自己写一个文件保存须要的数据,在这里我将文件保存在系统中的工程路径下。


输入需要保存的内容




保存完毕后红框内呈现之前保存的数据





        保存文件以后,文件被保存在了当前工程下 files 文件夹的路径下,这里说一下data文件夹 如果手机没有root 权限 用户是访问不到的,这种储存方式有一个麻烦的地方就是文件中保存的数据须要程序员自己去处理 , 好比文件中保存了很多字符串数据 但是我们只须要其中的一部分数据,这样就须要自己去写代码去从文件中拿需要的数据。




 
  1. public class FileActivity extends Activity {  
  2.     public final static String FILE_NAME = "a.txt";  
  3.      
  4.     /**File中储存数据的路径**/  
  5.     public final static String DATA_URL = "/data/data/";  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.     setContentView(R.layout.file);  
  9.     /**读取内容**/  
  10.     String content = loadFile();  
  11.     if(content == null) {  
  12.         content ="上次没有输入内容请输入";   
  13.     }  
  14.      String str  = "上次输入保存的内容的姓名为【 " +content + "】";  
  15.     final EditText editContent = ((EditText)findViewById(R.id.file_et0));  
  16.     editContent.setHint(str);  
  17.     Button button0 = (Button)findViewById(R.id.file_button0);  
  18.       
  19.     /**监听按钮点击后保存用户输入信息到file中**/  
  20.     button0.setOnClickListener(new  OnClickListener() {  
  21.         @Override  
  22.         public void onClick(View arg0) {  
  23.         /**拿到用户输入的信息**/  
  24.         String content = editContent.getText().toString();  
  25.         /**开始保存入file**/  
  26.         saveFile(content);  
  27.         ShowDialog("保存File文件成功");  
  28.         }  
  29.     });  
  30.       
  31.     Button button1 = (Button)findViewById(R.id.file_button1);  
  32.     /**监听按钮点击后清空file中内容**/  
  33.     button1.setOnClickListener(new  OnClickListener() {  
  34.         @Override  
  35.         public void onClick(View arg0) {  
  36.         cleanFile();  
  37.         ShowDialog("清空File文件成功");  
  38.         }  
  39.     });  
  40.       
  41.     Button button2 = (Button)findViewById(R.id.file_button2);  
  42.       
  43.     /**监听按钮点击后删除file文件**/  
  44.     button2.setOnClickListener(new  OnClickListener() {  
  45.         @Override  
  46.         public void onClick(View arg0) {  
  47.         File file = new File(DATA_URL + getPackageName().toString()  
  48.             + "/files", FILE_NAME);  
  49.         if (file.exists()) {  
  50.             file.delete();  
  51.         }  
  52.         ShowDialog("删除file文件成功");  
  53.         }  
  54.     });  
  55.       
  56.     super.onCreate(savedInstanceState);  
  57.     }  
  58.       
  59.     /** 
  60.      * 保存内容 
  61.      * @param str 
  62.      */  
  63.     public void saveFile(String str) {  
  64.     try {  
  65.         FileOutputStream outStream = this.openFileOutput(FILE_NAME,  
  66.             Context.MODE_WORLD_READABLE);  
  67.         outStream.write(str.getBytes());  
  68.         outStream.close();  
  69.     } catch (FileNotFoundException e) {  
  70.     } catch (IOException e) {  
  71.     }  
  72.     }  
  73.    
  74.     /** 
  75.      * 因为java删除文件内容只有一种实现方法,就是把整个文件重写,只是把须要删除的那一条记录去除掉  
  76.      */  
  77.     public void cleanFile() {  
  78.     //如果只须要删除文件中的一部分内容则须要在这里对字符串做一些操作   
  79.     String cleanStr = "";  
  80.     try {  
  81.         FileOutputStream outStream = this.openFileOutput(FILE_NAME,  
  82.             Context.MODE_WORLD_READABLE);  
  83.         outStream.write(cleanStr.getBytes());  
  84.         outStream.close();  
  85.     } catch (FileNotFoundException e) {  
  86.     } catch (IOException e) {  
  87.     }  
  88.   
  89.     }  
  90.       
  91.   
  92.       
  93.       
  94.     public String loadFile() {  
  95.     try {  
  96.         FileInputStream inStream = this.openFileInput(FILE_NAME);  
  97.         ByteArrayOutputStream stream = new ByteArrayOutputStream();  
  98.         byte[] buffer = new byte[1024];  
  99.         int length = -1;  
  100.         while ((length = inStream.read(buffer)) != -1) {  
  101.         stream.write(buffer, 0, length);  
  102.         }  
  103.         stream.close();  
  104.         inStream.close();  
  105.         return stream.toString();  
  106.     } catch (FileNotFoundException e) {  
  107.         e.printStackTrace();  
  108.     } catch (IOException e) {  
  109.     }  
  110.     return null;  
  111.     }  
  112.       
  113.     public void ShowDialog(String str) {  
  114.     AlertDialog.Builder builder = new AlertDialog.Builder(FileActivity.this);  
  115.     builder.setIcon(R.drawable.icon);  
  116.     builder.setTitle(str);  
  117.     builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
  118.         public void onClick(DialogInterface dialog, int whichButton) {  
  119.         finish();  
  120.         }  
  121.     });  
  122.     builder.show();  
  123.     }  
  124. }  

 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     >  
  8.     <ImageView android:id="@+id/file_image"  
  9.         android:layout_width="wrap_content"  
  10.          android:layout_height="wrap_content"  
  11.         android:src="@drawable/jay"  
  12.         android:layout_gravity="center"  
  13.         />  
  14.       
  15.     <EditText android:id="@+id/file_et0"  
  16.               android:layout_width="fill_parent"  
  17.               android:layout_height="wrap_content"  
  18.               android:hint="请输入需要保存的内容">  
  19.     </EditText>  
  20.     <Button   android:id="@+id/file_button0"  
  21.               android:layout_width="wrap_content"  
  22.               android:layout_height="wrap_content"  
  23.               android:text="保存入file">  
  24.     </Button>  
  25.     <Button   android:id="@+id/file_button1"  
  26.               android:layout_width="wrap_content"  
  27.               android:layout_height="wrap_content"  
  28.               android:text="清除file保存内容">  
  29.     </Button>  
  30.     <Button   android:id="@+id/file_button2"  
  31.               android:layout_width="wrap_content"  
  32.               android:layout_height="wrap_content"  
  33.               android:text="删除file文件">  
  34.     </Button>  
  35. </LinearLayout>  
linux
相关资讯       Android教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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