《第一行代码》第六章 文件存储项目Test
来源:互联网 发布:js的event对象 编辑:程序博客网 时间:2024/06/05 11:50
编者按:刚开始学习安卓,比较基础。从头到尾看郭神的书,一开始也是什么的都不会,比较着急。很多的方法什么的,代码什么的,import过程什么的,老是出错。然而回过头来看看,原来这么简单!很多人都是从这本书开始安卓生涯的。别说我,吻我,因为我只是代码的搬运工!
参考:《第一行代码》
文件存储是最基本的存储方式,因为其存储数据很随意,因此很多时候需要自己定义解析方式。就像,我从老家坐的火车到学校,那我回家也要坐火车。
文件存储主要两个核心知识,怎么去存储自己的数据,然后又是怎么去把自己的数据从文件中取出来!
下面是一个简单的test,自然的String数据。
肯定新建项目,新建布局,加载布局这是套路,然后在布局中拿到EditText控件,拿到里面的数据,存储,取出就ok.效果就是退出程序,再进来发现EditText还是刚才我们写的模样。
具体细节:getText().toString()方法肯定是来拿到数据的,但是什么时候进行存储最合适,肯定是在我们退出编辑之前的最后一秒呀,这里我们就退出活动。于是在onDestory()中进行save(),好了,当我们再次进入的时候会发生什么事情呢?在初始化活动的时候写个判断逻辑,判断我们的文件中是否有残留“草稿”,如果有,则显示出来,并使尽各种手段使用户酸爽,比如从末尾开始编辑(郭神),若没有,则新建文件就好啦。至于存储细节我实在是不想写了,别说话,看代码!(我在里面加了个对比)
java代码:
package com.example.fliepersistencetest;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import android.app.Activity;import android.app.Fragment.SavedState;import android.content.Loader;import android.os.Bundle;import android.text.TextUtils;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity{ private EditText editText,editText2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText=(EditText) findViewById(R.id.edit); editText2=(EditText) findViewById(R.id.edit1); editText2.setText(editText2.getText()); String inputTextString=load(); if(!TextUtils.isEmpty(inputTextString)){ editText.setText(inputTextString); editText.setSelection(inputTextString.length()); Toast.makeText(this, "你好", Toast.LENGTH_SHORT).show(); } } @Override protected void onDestroy() { super.onDestroy(); String edit1=editText.getText().toString(); save(edit1); } public void save(String inputText){ FileOutputStream out=null; BufferedWriter writer=null; try { out=openFileOutput("data", MODE_PRIVATE); writer=new BufferedWriter(new OutputStreamWriter(out)); writer.write(inputText); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(writer!=null){ try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public String load(){ FileInputStream inputStream=null; BufferedReader reader=null; StringBuilder contentBuilder=new StringBuilder(); try { inputStream=openFileInput("data"); reader=new BufferedReader(new InputStreamReader(inputStream)); String line=""; while((line=reader.readLine())!=null){ contentBuilder.append(line); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ if(reader!=null){ try { reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return contentBuilder.toString(); }}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/this_is_file_persistence" /> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/type_something_here"/> <TextView android:layout_marginTop="50dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/this_is_no_file_persistence" /> <EditText android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:hint="@string/type_something_here" /></LinearLayout>
0 0
- 《第一行代码》第六章 文件存储项目Test
- 《第一行代码》第六章 SharedPreferences存储Test
- 《第一行代码》第六章 SQLite存储Test
- 第一行代码笔记,第六章-----详解数据存储
- 第一行代码-6.2 文件存储
- 第一行代码第六日
- 《第一行代码》读书笔记(第五章,第六章)
- 【Android】《第一行代码—Android》第六章总结
- 《第一行代码》第六章 记住密码功能
- 第一行代码 第六章 数据储存方案
- 第一行代码 第六章 数据储存方案
- 第一行代码 第六章 数据储存方案
- 第一行代码 第六章 数据储存方案
- [Android][第一行代码][第 6 章 数据存储]
- 数据存储之文件存储——第一行代码Android学习笔记
- 第一行代码笔记3:数据存储
- Android 数据存储 《第一行代码》
- 第一行代码-6.3 SharedPreferences存储
- 复制链接地址
- 03 VoLTE - Initial LTE Attach
- leetcode笔记-层序遍历
- Tomcat作为Servlet容器的基本功能
- Eclisep简单安装问题
- 《第一行代码》第六章 文件存储项目Test
- Stanford ex7: K-means Clustering and Principal Component Analysis
- SDOI2016 Round 1游记
- HDU--2096 小明A+B
- OpenStack mitaka DevStack 部署
- 数据库设计 Step by Step (3)——基本ER模型构件
- [JAVA修炼之路七]-JVM 垃圾回收器
- windows上帝模式
- 143. Reorder List