免费建站网站黄金网站,淘客网站开发流程,海口快速建站模板,群晖 wordpress 编辑1. 数据存储
1.1 知识点
#xff08;1#xff09;掌握Android数据存储的分类#xff1b;
#xff08;2#xff09;可以使用SharedPreferences存储数据。
1.2 具体内容
对于我们数据的存储而言#xff0c;Android一共提供了5个数据存储的方式#xff1a;SharedPrefe…1. 数据存储
1.1 知识点
1掌握Android数据存储的分类
2可以使用SharedPreferences存储数据。
1.2 具体内容
对于我们数据的存储而言Android一共提供了5个数据存储的方式SharedPreferences存储、文件存储方式、Sqlite存储、Content Provider存储、网络存储。 在Android之中操作都需要使用Activity程序进行支持本次课程我们只关注操作方法所有不做过多的页面展示。
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalLinearLayout android:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text请输入键/EditTextandroid:idid/usernameKeyandroid:layout_width200pxandroid:layout_heightwrap_content //LinearLayoutLinearLayout android:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:text请输入值/EditTextandroid:idid/usernameandroid:layout_width200pxandroid:layout_heightwrap_content //LinearLayoutButton android:idid/mybutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:text保存/
/LinearLayout现在的关键问题就在于编写Activity程序。
package com.example.sharedpreferences;import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;public class SharedPreferencesActivity extends Activity {private TextView username null;private TextView age null;public static final String FILMNAME wanczy;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_shared_preferences);this.age (TextView) super.findViewById(R.id.age);this.username (TextView) super.findViewById(R.id.username);SharedPreferences share super.getSharedPreferences(FILMNAME, Activity.MODE_PRIVATE);this.username.setText(用户名 share.getString(username, 无所谓默认值));//根据键取得值并放入到TextView中this.age.setText(年龄 share.getInt(年龄,0));}}
和在Java中使用属性存储操作上有相似之处但是Java中的属性存储已经过时现在存储比较流行的是xml存储。此时程序就已经完成了默认情况下存储文件都会保存在手机里面后缀为.xml现在程序已经可以保存当然也可以读取咯。
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationvertical TextViewandroid:idid/usernameandroid:layout_widthwrap_contentandroid:layout_heightwrap_content /TextViewandroid:idid/ageandroid:layout_widthwrap_contentandroid:layout_heightwrap_content/
/LinearLayout
package com.example.sharedpreferences;import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;public class SharedPreferencesActivity extends Activity {private TextView username null;private TextView age null;public static final String FILMNAME wanczy;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_shared_preferences);this.age (TextView) super.findViewById(R.id.age);this.username (TextView) super.findViewById(R.id.username);SharedPreferences share super.getSharedPreferences(FILMNAME, Activity.MODE_PRIVATE);this.username.setText(用户名 share.getString(username, 无所谓默认值));//根据键取得值并放入到TextView中this.age.setText(年龄 share.getInt(age,0));}}对于SharedPreferences存储而言并没有太多复杂操作实际应用当中使用SharedPreferences可以保存一些配置信息例如你正在看小说希望关闭之后下次打开能够在你最后浏览的进度点那么这种情况下就可以使用SharedPreferences进行保存 。
1.3 小结
1SharedPreferences可以实现简单的数据存储功能实现可以利用super.getSharedPreferences()方法取得实例
2. 文件存储
2.1 知识点
1掌握Activity对文件存储的若干操作
2可以实现文件的保存和读取操作。
2.2 具体内容
对于文件存储这块必须要先掌握IO的基本操作InputStreamOutputStream。 了解一下IO流对文件的操作
·使用File找到一个指定的文件
·使用字节流或者字符流的子类为父类进行实例化
·完成输入/输出的操作
·关闭流 举例向文件中写入内容—输出我们的输入输出是对于程序而言。
范例本次还是以文件保存为主不再进行页面的编写。
package com.example.filesave;import java.io.FileOutputStream;
import java.io.PrintStream;import android.app.Activity;
import android.os.Bundle;public class FileSaveActivity extends Activity {public static final String FILENAME wanczy.txt;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);FileOutputStream output null;PrintStream out null;try {output super.openFileOutput(FILENAME, Activity.MODE_PRIVATE);//使用Activity提供的方法创建了一个文件字节输出流//在IO操作中打印流操作是最方便的out new PrintStream(output);out.print(姓名毛栗子);out.print(年龄30);out.print(地址兰州市庆阳路128号);} catch (Exception e) {} finally{out.close();}}
}
那么现在既然可以写入内容到文件当然我们也可以将内容从文件中拿出来那么对于读取文件那肯定需要将文件内容放入到TextView中显示。
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalTextViewandroid:idid/msgandroid:layout_widthmatch_parentandroid:layout_heightmatch_parent/
/LinearLayout以后的读取的内容就放在msg这个文本显示组件上如果大家学习过Java的话应该很清楚使用PrintStream对于输出来说很方便对于输入呢使用什么最方便呢使用扫描类肯定是最方便的就是Scanner
package com.example.filesave;import java.io.FileInputStream;
import java.util.Scanner;import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;public class FileSaveActivity extends Activity {private static final String FILENAME wanczy.txt;private TextView msg null;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);this.msg (TextView) super.findViewById(R.id.msg);FileInputStream input null;Scanner sc null;try {input super.openFileInput(FILENAME);//使用Activity提供的方法创建了一个文件字节输出流//在IO操作中打印流操作是最方便的sc new Scanner(input);String content sc.next();//读取数据this.msg.setText(读取的信息为content);} catch (Exception e) {} finally{sc.close();}}
}
以上的这种写法就是比较标准的程序。 范例向SD卡上保存文件
package com.example.filesave;import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;import android.app.Activity;
import android.os.Bundle;public class FileSaveActivity extends Activity {public static final String FILENAME /mnt/sdcard/wanczy/jjm/wanczy.txt;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);FileOutputStream output null;PrintStream out null;try {File file new File(FILENAME);if(!file.getParentFile().exists()){//如果文件夹不存在file.mkdirs();}output new FileOutputStream(file,true);//表示创建了一个文件的字节输出流 这种写法不能进行文件内容的追加//在IO操作中打印流操作是最方便的out new PrintStream(output);out.print(姓名毛栗子);out.print(年龄30);out.print(地址兰州市庆阳路128号);} catch (Exception e) {} finally{if(null ! out){out.close();}}}}范例向SD卡上保存文件
现在虽然程序写好了既然是对SD card的操作那么一定需要具备操作sdcard的权限。
uses-permission android:nameandroid.permission.WRITE_EXTERNAL_STORAGE/ 现在我们只需要使用此类去判断sdcard是否存在。
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//表示存储目录能够进行你读写操作System.out.println(表示sdcard存在);}
package com.example.filesave;import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;public class FileSaveActivity extends Activity {public static final String FILENAME /mnt/sdcard/wanczy/jjm/wanczy.doc;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 表示存储目录能够进行你读写操作FileOutputStream output null;PrintStream out null;try {File file new File(FILENAME);if (!file.getParentFile().exists()) {// 如果文件夹不存在file.mkdirs();}output new FileOutputStream(file, true);// 表示创建了一个文件的字节输出流// 这种写法不能进行文件内容的追加// 在IO操作中打印流操作是最方便的out new PrintStream(output);out.print(《幽窗小记》 宠若不惊 闲看庭前花开花落 去留无意 漫随天外云卷云舒);} catch (Exception e) {} finally {if (null ! out) {out.close();}}}else{Toast.makeText(this, sdcard不存在请使用其他保存路径, Toast.LENGTH_SHORT).show();}}}这个程序更加适合在真机上进行操作。现在已经完成输出的功能现在进行一些输入的显示。
package com.example.filesave;import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;public class FileSaveActivity extends Activity {private static final String FILENAME wanczy.doc;private static final String DIR CSDN/mlz;private TextView msg null;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);this.msg (TextView) super.findViewById(R.id.msg);if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {FileInputStream input null;Scanner sc null;try {File file new File(Environment.getExternalStorageDirectory().toString()File.separatorDIRFile.separatorFILENAME);input new FileInputStream(file);sc new Scanner(input);while(sc.hasNext()){this.msg.append( sc.next());}} catch (Exception e) {} finally {sc.close();}}else{Toast.makeText(this, sdcard不存在请使用其他保存路径, Toast.LENGTH_SHORT).show();}}
}以上保存在sdcard的程序与之前的不保存在sdcard并没有本质的区别。 package com.example.filesave;import java.io.InputStream;
import java.util.Scanner;import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;public class FileSaveActivity extends Activity {private TextView msg null;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_file_save);this.msg (TextView) super.findViewById(R.id.msg);Resources res super.getResources();//取得Resources对象InputStream in res.openRawResource(R.raw.wanczy);//将资源文件加入到字节输入流、Scanner sc null;StringBuffer sb new StringBuffer();try {sc new Scanner(in);while(sc.hasNext()){sb.append(sc.next()\n);}}catch(Exception e){}finally{sc.close();}this.msg .setText(sb.toString());}}2.3 小结
1使用文件存储可以保存更加丰富的数据
2在Android之中可以使用XML的DOM和SAX解析方式进行文件操作
3在Android之中提供了PULL解析用于完成XML解析
4JSON可以进行简便的信息传送性能更高
5可以将要读取的文件配置到项目的res文件目录之中这样可以采用Resource直接进行资源文件的读取。