您的位置:首页 > 移动开发 > Android开发

Android数据存车和访问

2015-06-29 22:41 411 查看
一、知识了解:

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>


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: