Android数据存车和访问
2015-06-29 22:41
411 查看
一、知识了解:
1.什么是数据流?
数据流(data stream)最初是通信领域使用的概念,代表传输中所使用的信息的数字编码信号序列。然而,我们所提到的数据流概念与此不同。这个概念最初在1998年由Henzinger在文献87中提出,他将数据流定义为“只能以事先规定好的顺序被读取一次的数据的一个序列”。数据流分为输入流(InputStream)和输出流(OutputStream)两类。
2.什么是输入流?
在Java中,能够读取一个字节序列的对象就称作一个输入流。输入流只能读不能写,可从键盘或文件中获得数据。
3.什么是输出流?
输出流可向显示器、打印机或文件中传输数据。
二.书写代码:
1.写测试类:FileServiceTest.java
2.FileService.java
3.进行配置清单文件:
4.进行Android测试:
5.测试结果:
6.书写控制层代码:
1.什么是数据流?
数据流(data stream)最初是通信领域使用的概念,代表传输中所使用的信息的数字编码信号序列。然而,我们所提到的数据流概念与此不同。这个概念最初在1998年由Henzinger在文献87中提出,他将数据流定义为“只能以事先规定好的顺序被读取一次的数据的一个序列”。数据流分为输入流(InputStream)和输出流(OutputStream)两类。
2.什么是输入流?
在Java中,能够读取一个字节序列的对象就称作一个输入流。输入流只能读不能写,可从键盘或文件中获得数据。
3.什么是输出流?
输出流可向显示器、打印机或文件中传输数据。
二.书写代码:
1.写测试类:FileServiceTest.java
<span style="font-family:SimSun;">package com.example.fileoperatedemo.test; import com.example.fileop 4000 eratedemo.service.FileService; import android.test.AndroidTestCase; public class FileServieTest extends AndroidTestCase { public void testSave(){ <span style="white-space: pre;"></span>FileService fileService=new FileService(getContext(), "test.txt"); <span style="white-space: pre;"></span>fileService.save("hello world"); } } </span>
2.FileService.java
<span style="font-family:SimSun;">package com.example.fileoperatedemo.service;</span> <span style="font-family:SimSun;">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.OutputStream; import java.io.OutputStreamWriter; import android.content.Context; public class FileService { private Context context; private String fileName; public FileService(Context context,String fileName){ this.context=context; this.fileName=fileName; } public boolean save(String content){ BufferedWriter bw=null;<span style="color:#0800;line-height: 1.5 !important; white-space: pre-wrap;">//</span><span style="color:#0800;line-height: 1.5 !important; white-space: pre-wrap;"> 缓冲区声明</span> boolean isSaveSucceed=false; try { FileOutputStream fos=context.openFileOutput(fileName,context.MODE_PRIVATE); OutputStreamWriter writer=new OutputStreamWriter(fos); bw=new BufferedWriter(writer); bw.write(content); isSaveSucceed=true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(bw!=null) try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } return isSaveSucceed; } public String read(){ String line; StringBuilder sb=new StringBuilder(); BufferedReader br=null; try { FileInputStream fis=context.openFileInput(fileName); br=new BufferedReader(new InputStreamReader(fis)); while((line=br.readLine())!=null){ sb.append(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } </span> }
3.进行配置清单文件:
<span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;line-height: 23px; font-size: 13px;"><?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.fileoperatedemo" android:versionCode="1" android:versionName="1.0" > < uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> < application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > < activity android:name="com.example.fileoperatedemo.MainActivity" android:label="@string/app_name" > < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < uses-library android:name="android.test.runner"/> < /application> < instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.fileoperatedemo" > < /instrumentation> </span>
4.进行Android测试:
5.测试结果:
6.书写控制层代码:
<p><span style="line-height: 23px; font-family: Verdana,Geneva,Arial,Helvetica,sans-serif; font-size: 13px;"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;line-height: 23px; font-size: 13px;"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;line-height: 23px; font-size: 13px;"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;line-height: 23px; font-size: 13px;">package com.example.fileoperatedemo; import com.example.fileoperatedemo.service.FileService; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; //控制层 public class MainActivity extends Activity { private EditText etContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } private void initViews() { etContent=(EditText) findViewById(R.id.etContent); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void save(View view){ //从V获取数据 String content=etContent.getText().toString(); //调用模型层M进行处理 FileService fileService=new FileService(this, "data.txt"); boolean isSavesucceed=fileService.save(content); if(isSavesucceed){ Toast.makeText(this, "恭喜你,保存成功了", Toast.LENGTH_LONG).show(); } } } </span></span></span></span></p><p><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;line-height: 23px; font-size: 13px;"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;line-height: 23px; font-size: 13px;"><strong><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;line-height: 23px; font-size: 13px;"></span></strong></span></span></p>
相关文章推荐
- Android自启动注意事项
- 修改Android系统内/system目录权限使其可读写
- 一个Android进程到底有多少个context对象呢
- Android系统文件夹功能介绍
- Android图像处理之Bitmap类
- Android进程与生命周期介绍
- 创建Material Design风格的Android应用--应用主题
- Android设置Listview每个item的间距
- ECClient 红孩子android客户端listview图片加载(优化)核心代码分析
- Android 源代码编后的目录分析
- Android的Audio系统
- android开发利用tablelayout布局实现登录界面
- Android jni开发资料--NDK环境搭建
- Android通过用代码画虚线椭圆边框背景来学习一下shape的用法
- Android通过用代码画虚线椭圆边框背景来学习一下shape的用法
- Android开发博客重新开启
- Android AsyncTaskLoader需要注意的问题
- Xamarin.Android开发实践(一)
- Android Studio导入project慢的原因
- android 虚拟键盘的显示与隐藏问题