android文件读取 DataActivity.java
1 package com.pptv.file;
2
3 import java.io.FileNotFoundException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.os.Bundle;
10 import android.util.Log;
11 import android.view.View;
12 import android.widget.Button;
13 import android.widget.EditText;
14 import android.widget.TextView;
15 import android.widget.Toast;
16
17 import com.pptv.service.FileService;
18
19 public class DataActivity extends Activity {
20privateEditTextfilenameText;
21privateEditTextcontentText;
22privateTextViewresultView;
23privatestaticfinalStringTAG="DataActivity";
24
25@Override
26publicvoidonCreate(BundlesavedInstanceState){
27super.onCreate(savedInstanceState);
28setContentView(R.layout.main);
29filenameText=(EditText)findViewById(R.id.edittext_filename);
30contentText=(EditText)findViewById(R.id.textconetent);
31resultView=(TextView)findViewById(R.id.textview_result);
32Buttonbutton=(Button)findViewById(R.id.button_save);
33ButtonbuttonShow=(Button)findViewById(R.id.button_open);
34button.setOnClickListener(listener);
35buttonShow.setOnClickListener(listener);
36}

37
38privateView.OnClickListenerlistener=newView.OnClickListener(){
39
40@Override
41publicvoidonClick(Viewv){
42Buttonbutton=(Button)v;
43Stringfilename=filenameText.getText().toString();
44switch(button.getId()){
45caseR.id.button_save:
46intresId=R.string.sucess;
47Stringcontent=contentText.getText().toString();
48OutputStreamoutStream;
49try{
50outStream=DataActivity.this.openFileOutput(filename,
51Context.MODE_APPEND);
52FileService.save(outStream,content);
53}
catch(Exceptione){
54Log.e(TAG,e.toString());
55resId=R.string.error;
56}

57Toast.makeText(DataActivity.this,resId,Toast.LENGTH_LONG)
58.show();
59break;
60
61caseR.id.button_open:
62try{
63InputStreaminStream=DataActivity.this
64.openFileInput(filename);
65Stringtext=FileService.read(inStream,null);
66resultView.setText(text);
67}
catch(Exceptione){
68Log.e(TAG,e.toString());
69Toast.makeText(DataActivity.this,"读取失败",Toast.LENGTH_LONG).show();
70}

71break;
72
73default:
74break;
75}

76
77}

78}
;
79
80}

81

FileServiceTest.java ---单元测试模块
1 package com.pptv.file;
2
3 import java.io.InputStream;
4 import java.io.OutputStream;
5
6 import android.content.Context;
7 import android.test.AndroidTestCase;
8 import android.util.Log;
9
10 import com.pptv.service.FileService;
11
12 public class FileServiceTest extends AndroidTestCase {
13privatestaticfinalStringTAG="FileServiceTest";
14
15publicvoidtestSave()throwsException{
16OutputStreamoutStream=this.getContext().openFileOutput("songwei.txt",Context.MODE_PRIVATE);
17FileService.save(outStream,"neironga!!!");
18}

19
20publicvoidtestRead()throwsException{
21InputStreaminStream=this.getContext().openFileInput("songwei.txt");
22Stringcontent=FileService.read(inStream,null);
23Log.i(TAG,content);
24}

25}

26

FileService.java ----javabean模块
1 package com.pptv.service;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6
7 public class FileService {
8/***//**
9*保存数据~~
10*@paramoutStream
11*@paramcontent
12*@throwsException
13*/

14publicstaticvoidsave(OutputStreamoutStream,Stringcontent)throwsException{
15outStream.write(content.getBytes());
16outStream.close();
17}

18
19/***//**
20*读取数据~~
21*@paraminStream
22*@paramcontent
23*@return
24*@throwsException
25*/

26publicstaticStringread(InputStreaminStream,Stringcontent)throwsException{
27ByteArrayOutputStreamoutStream=newByteArrayOutputStream();
28byte[]buffer=newbyte[1024];
29intlen=-1;
30while((len=inStream.read(buffer))!=-1){
31outStream.write(buffer,0,len);
32}

33byte[]data=outStream.toByteArray();
34outStream.close();
35inStream.close();
36returnnewString(data);
37}

38}

39

main.xml ---布局文件
1 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < LinearLayoutxmlns:android = " http://schemas.android.com/apk/res/android "
3 android:orientation = " vertical "
4 android:layout_width = " fill_parent "
5 android:layout_height = " fill_parent "
6 >
7 < RelativeLayoutxmlns:android = " http://schemas.android.com/apk/res/android "
8 android:orientation = " vertical "
9 android:layout_width = " fill_parent "
10 android:layout_height = " wrap_content " >
11 < TextView
12 android:id = " @+id/filename "
13 android:layout_width = " wrap_content "
14 android:layout_height = " 10pt "
15
16 android:text = " @string/file_name "
17 />
18
19 < EditText
20 android:layout_width = " 100pt "
21 android:layout_height = " wrap_content "
22 android:layout_toRightOf = " @id/filename "
23 android:layout_marginLeft = " 10pt "
24 android:id = " @+id/edittext_filename "
25 />
26 </ RelativeLayout >
27
28 < TextView
29 android:layout_width = " wrap_content "
30 android:layout_height = " wrap_content "
31 android:text = " @string/txtcontent "
32 />
33 < EditText
34 android:id = " @+id/textconetent "
35 android:layout_width = " fill_parent "
36 android:layout_height = " wrap_content "
37 android:minLines = " 4 "
38 />
39
40 < RelativeLayoutxmlns:android = " http://schemas.android.com/apk/res/android "
41 android:orientation = " vertical "
42 android:layout_width = " fill_parent "
43 android:layout_height = " wrap_content " >
44 < Button
45 android:id = " @+id/button_save "
46 android:layout_width = " wrap_content "
47 android:layout_height = " wrap_content "
48 android:text = " @string/button_save "
49 />
50 < Button
51 android:id = " @+id/button_open "
52 android:layout_width = " wrap_content "
53 android:layout_height = " wrap_content "
54 android:layout_toRightOf = " @id/button_save "
55 android:text = " @string/button_open "
56 />
57 </ RelativeLayout >
58
59 < TextView
60 android:id = " @+id/textview_result "
61 android:layout_width = " fill_parent "
62 android:layout_height = " wrap_content "
63 />
64
65 </ LinearLayout >

strings.xml
1 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < resources >
3 < stringname = " txtcontent " > 文本内容: </ string >
4 < stringname = " app_name " > 数据保存 </ string >
5 < stringname = " file_name " > 文件名称: </ string >
6 < stringname = " button_save " > 保存 </ string >
7 < stringname = " error " > 保存失败 </ string >
8 < stringname = " sucess " > 保存成功 </ string >
9 < stringname = " button_open " > 打开文件 </ string >
10 </ resources >
11

AndroidManifest.xml
1 <? xmlversion = " 1.0 " encoding = " utf-8 " ?>
2 < manifestxmlns:android = " http://schemas.android.com/apk/res/android "
3 package = " com.pptv.file "
4 android:versionCode = " 1 "
5 android:versionName = " 1.0 " >
6 < applicationandroid:icon = " @drawable/icon " android:label = " @string/app_name " >
7 // 测试权限
8 < uses - libraryandroid:name = " android.test.runner " />
9
10 < activityandroid:name = " .DataActivity "
11 android:label = " @string/app_name " >
12 < intent - filter >
13 < actionandroid:name = " android.intent.action.MAIN " />
14 < categoryandroid:name = " android.intent.category.LAUNCHER " />
15 </ intent - filter >
16 </ activity >
17
18 </ application >
19 < uses - sdkandroid:minSdkVersion = " 4 " />
20 // 测试权限
21 < instrumentationandroid:name = " android.test.InstrumentationTestRunner "
22 android:targetPackage = " com.pptv.file " android:label = " TestsforMyApp " />
23 </ manifest >

更多相关文章

  1. 将 android sql文件放生成在sd卡
  2. android 一些数据转换方法
  3. Android写本地文件
  4. android 工具类 数据库管理
  5. 不使用布局文件,代码中自定义界面
  6. 清除app数据等同于设置中的清除数据
  7. android manifest文件结构

随机推荐

  1. Android(安卓)XML解析器的问题
  2. Android(安卓)SystemProperties
  3. Eclipse将android 类编译为jar类库
  4. 二,HelloWorld 及源码关联 & Manifest 类
  5. [Android]AndroidDesign中ActionBar探究1
  6. Android中Thread、Handler、Looper、Mess
  7. Android架构分析之Android智能指针(一)
  8. Android中发送Http请求实例
  9. Android-编辑需求demo实现
  10. [转]android 系统权限大全的简介与内容