android 文件的读取与写入以及TextView

本文提供了一个Android应用中文件读写的示例代码,包括文本文件的保存与加载功能,并展示了如何使用Android内置资源读取静态文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. packagetraining.android.com;
  2. importjava.io.FileNotFoundException;
  3. importjava.io.IOException;
  4. importjava.io.InputStream;
  5. importjava.io.OutputStream;
  6. importandroid.app.Activity;
  7. importandroid.os.Bundle;
  8. importandroid.os.Handler;
  9. importandroid.os.Message;
  10. importandroid.view.KeyEvent;
  11. importandroid.view.Menu;
  12. importandroid.view.MenuItem;
  13. importandroid.view.View;
  14. importandroid.view.View.OnClickListener;
  15. importandroid.widget.Button;
  16. importandroid.widget.EditText;
  17. importandroid.widget.ScrollView;
  18. importandroid.widget.TextView;
  19. importandroid.widget.Toast;
  20. publicclassfileWriteReadextendsActivityimplementsOnClickListener{
  21. /**Calledwhentheactivityisfirstcreated.*/
  22. privateEditTextedit_Text;//编缉文本框
  23. privateButtonbtn_Save;//保相聚按钮
  24. privateStringtext_of_input;//输入文本值
  25. privateOutputStreamos;//输出流
  26. privateTextViewshowMyText;//显示文本
  27. privateButtonbtnOpenText,btnCleanText;//打开与清空按钮事件
  28. privateButtonbtnOpenRaw;
  29. privateStringText_of_output;//输入出文本
  30. privateInputStreamis;//输入流
  31. privateInputStreamiputRaw;//静态文件流
  32. privatebyte[]b;
  33. privateScrollViewscroll;
  34. privateintori=0;
  35. /**
  36. *页面的初始化
  37. *
  38. */
  39. @Override
  40. publicvoidonCreate(BundlesavedInstanceState){
  41. super.onCreate(savedInstanceState);
  42. //setContentView(R.layout.main);
  43. showLayout("main");
  44. UIinit("main");
  45. Logic("main");
  46. }
  47. /**
  48. *通过页面名称去显示各页面信息创建人:周昕创建时间:2010-4-8方法名称:showLayout
  49. *
  50. *@paramlayoutName
  51. */
  52. privatevoidshowLayout(StringlayoutName){
  53. if(layoutName.equals("main")){
  54. setContentView(R.layout.main);
  55. }
  56. if(layoutName.equals("openfile")){
  57. setContentView(R.layout.openfile);
  58. }
  59. }
  60. /**
  61. *通过页面名称去初始化控件
  62. *
  63. *@paramuiName
  64. *创建人:周昕&&&&&&&&创建时间:2010-4-8
  65. */
  66. privatevoidUIinit(StringuiName){
  67. if(uiName.equals("main")){
  68. edit_Text=(EditText)findViewById(R.id.Edit_text);
  69. btn_Save=(Button)findViewById(R.id.Button_save);
  70. }
  71. if(uiName.equals("openfile")){
  72. btnOpenText=(Button)findViewById(R.id.Button_openTxt);
  73. btnCleanText=(Button)findViewById(R.id.Button_clean);
  74. btnOpenRaw=(Button)findViewById(R.id.Button_openRaw);
  75. showMyText=(TextView)findViewById(R.id.TextView_showTxt);
  76. }
  77. }
  78. /**
  79. *通过页面名称去对各Button指定事件
  80. *
  81. *@parampageName
  82. *创建人:周昕********创建时间:2010-4-8
  83. */
  84. privatevoidLogic(StringpageName){
  85. if(pageName.equals("main")){
  86. btn_Save.setOnClickListener(this);
  87. }
  88. if(pageName.equals("openfile")){
  89. btnOpenText.setOnClickListener(this);
  90. btnCleanText.setOnClickListener(this);
  91. btnOpenRaw.setOnClickListener(this);
  92. }
  93. }
  94. /**
  95. *在Toast中显示指定的字段
  96. *
  97. *@paramstrTitle
  98. */
  99. privatevoidNoteDebug(StringstrTitle){
  100. Toast.makeText(this,strTitle,Toast.LENGTH_SHORT).show();
  101. }
  102. @Override
  103. publicvoidonClick(Viewv){
  104. //TODOAuto-generatedmethodstub
  105. switch(v.getId()){
  106. caseR.id.Button_save:
  107. NoteDebug("文件保存");
  108. text_of_input=edit_Text.getText().toString();
  109. try{
  110. os=this.openFileOutput("txtME",MODE_PRIVATE);
  111. os.write(text_of_input.getBytes());
  112. }catch(FileNotFoundExceptione){
  113. NoteDebug("文件关闭失败"+e);
  114. }catch(IOExceptione){
  115. NoteDebug("文件写入失败"+e);
  116. }finally{
  117. try{
  118. os.close();
  119. }catch(IOExceptione){
  120. NoteDebug("关闭文件失败"+e);
  121. }
  122. }
  123. edit_Text.setText("");
  124. break;
  125. caseR.id.Button_openTxt:
  126. NoteDebug("打开文件");
  127. try{
  128. is=this.openFileInput("txtME");
  129. b=newbyte[1024];
  130. intlength=is.read(b);
  131. Text_of_output=newString(b);
  132. setTitle("文件字数"+length);
  133. showMyText.setText(Text_of_output);
  134. }catch(FileNotFoundExceptione){
  135. NoteDebug("文件打开失败"+e);
  136. }catch(IOExceptione){
  137. //TODOAuto-generatedcatchblock
  138. NoteDebug("文件读取失败"+e);
  139. }finally{
  140. try{
  141. is.close();
  142. }catch(IOExceptione){
  143. NoteDebug("文件关闭失败"+e);
  144. }
  145. }
  146. break;
  147. caseR.id.Button_openRaw:
  148. NoteDebug("打开静态文件");
  149. try{
  150. iputRaw=this.getResources().openRawResource(R.raw.filetext);
  151. b=newbyte[102400];
  152. intlength=iputRaw.read(b);
  153. Text_of_output="";
  154. Text_of_output=newString(b);
  155. setTitle("静态文件字数"+length);
  156. showMyText.setText(Text_of_output);
  157. showMyText.setHorizontallyScrolling(true);
  158. scroll=(ScrollView)findViewById(R.id.scroll);
  159. scroll.smoothScrollBy(ori,0);
  160. }catch(FileNotFoundExceptione){
  161. NoteDebug("静态文件打开失败"+e);
  162. }catch(IOExceptione){
  163. //TODOAuto-generatedcatchblock
  164. NoteDebug("静态文件读取失败"+e);
  165. }finally{
  166. try{
  167. iputRaw.close();
  168. }catch(IOExceptione){
  169. NoteDebug("静态文件关闭失败"+e);
  170. }
  171. }
  172. break;
  173. caseR.id.Button_clean:
  174. NoteDebug("清空文件");
  175. showMyText.setText("");
  176. NoteDebug("清空");
  177. break;
  178. }
  179. }
  180. publicbooleanonCreateOptionsMenu(Menumenu){
  181. menu.add(0,1,1,"edit");
  182. menu.add(0,2,2,"open");
  183. menu.add(0,3,3,"clear");
  184. returnsuper.onCreateOptionsMenu(menu);
  185. }
  186. publicbooleanonOptionsItemSelected(MenuItemitems){
  187. switch(items.getItemId()){
  188. case1:
  189. showLayout("main");
  190. UIinit("main");
  191. Logic("main");
  192. NoteDebug("编缉文件");
  193. break;
  194. case2:
  195. showLayout("openfile");
  196. UIinit("openfile");
  197. Logic("openfile");
  198. NoteDebug("打开件");
  199. break;
  200. case3:
  201. showLayout("openfile");
  202. UIinit("openfile");
  203. Logic("openfile");
  204. NoteDebug("清空文件");
  205. break;
  206. }
  207. returnsuper.onOptionsItemSelected(items);
  208. }
  209. privateHandlermessage=newHandler(){
  210. publicvoidhandleMessage(Messagemsg){
  211. doScrow();
  212. }
  213. };
  214. publicclassTimerLoopimplementsRunnable{
  215. @Override
  216. publicvoidrun(){
  217. //TODOAuto-generatedmethodstub
  218. while(true){
  219. loop(500);
  220. message.sendEmptyMessage(0);
  221. }
  222. }
  223. }
  224. //因为sleep()似乎不好用所以采用这种方法计时
  225. publicvoidloop(longi){
  226. longj=i;
  227. while(j>0){
  228. j=j-1;
  229. }
  230. }
  231. publicbooleanonKeyDown(intkeyCode,KeyEventmsg){
  232. //Threadloop=newThread(newTimerLoop());
  233. //loop.start();
  234. returnsuper.onKeyDown(keyCode,msg);
  235. }
  236. publicvoiddoScrow(){
  237. intnow=scroll.getScrollY();
  238. if(ori==now){
  239. scroll.scrollTo(now,0);
  240. ori=-1;
  241. }
  242. else{
  243. scroll.smoothScrollBy(10,10);
  244. ori=now;
  245. }
  246. }
  247. }

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. <EditText
  8. android:id="@+id/Edit_text"
  9. android:layout_width="fill_parent"
  10. android:layout_height="350px">
  11. </EditText>
  12. <Button
  13. android:text="保存"
  14. android:id="@+id/Button_save"
  15. android:layout_width="80px"
  16. android:layout_height="wrap_content">
  17. </Button>
  18. </LinearLayout>

open.xml

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <AbsoluteLayoutandroid:id="@+id/openlayout"
  3. android:layout_width="fill_parent"android:layout_height="fill_parent"
  4. xmlns:android="http://schemas.android.com/apk/res/android">
  5. <!--<ScrollViewandroid:layout_weight="1"android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"android:scrollbars="vertical"
  7. android:fadingEdge="vertical"android:id="@+id/scroll">
  8. -->
  9. <ScrollView
  10. android:id="@+id/scroll"
  11. android:layout_width="fill_parent"
  12. android:layout_height="300dip">
  13. <LinearLayoutandroid:layout_width="fill_parent"
  14. android:layout_height="380px">
  15. <TextViewandroid:id="@+id/TextView_showTxt"
  16. android:layout_width="314px"android:layout_height="373px"
  17. android:scrollbars="vertical"android:fadingEdge="vertical"
  18. android:autoText="true"android:layout_x="3px"android:layout_y="3px"
  19. >
  20. <!--android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"-->
  21. </TextView>
  22. </LinearLayout>
  23. </ScrollView>
  24. <Buttonandroid:id="@+id/Button_openTxt"android:layout_width="80px"
  25. android:layout_height="wrap_content"android:text="打开"
  26. android:layout_x="2px"android:layout_y="378px">
  27. </Button>
  28. <Buttonandroid:id="@+id/Button_openRaw"android:layout_width="80px"
  29. android:layout_height="wrap_content"android:text="静态文件"
  30. android:layout_x="102px"android:layout_y="378px">
  31. </Button>
  32. <Buttonandroid:id="@+id/Button_clean"android:layout_width="80px"
  33. android:layout_height="wrap_content"android:text="清空"
  34. android:layout_x="239px"android:layout_y="378px">
  35. </Button>
  36. </AbsoluteLayout>

AndroidMainfest.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="training.android.com"android:versionCode="1"
  4. android:versionName="1.0">
  5. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  6. <activityandroid:name=".fileWriteRead"android:label="@string/app_name">
  7. <intent-filter>
  8. <actionandroid:name="android.intent.action.MAIN"/>
  9. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  10. </intent-filter>
  11. </activity>
  12. </application>
  13. <uses-sdkandroid:minSdkVersion="3"/>
  14. <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  15. <uses-permissionandroid:name="android.permission.MODE_WORLD_READABLE"></uses-permission>
  16. <uses-permissionandroid:name="android.permission.MODE_WORLD_WRITEABLE"></uses-permission>
  17. </manifest>

http://www.cnblogs.com/maoan/archive/2011/01/18/1938278.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值