`
isiqi
  • 浏览: 16842221 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

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

阅读更多
  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



分享到:
评论

相关推荐

    Android应用程序中读写txt文本文件的基本方法讲解

    在Android应用程序中,读写txt文本文件是常见的需求,特别是在数据持久化或用户交互时。本文将详细讲解如何使用`context.openFileInput()`和`context.openFileOutput()`这两个方法来实现这一功能。这两个方法属于`...

    Android读取本地json文件的方法(解决显示乱码问题)

    - **Android文件操作**:Android提供了多种文件操作方法,包括读写文件、文件流操作等。 - **SD卡操作**:Android允许应用在外部存储(如SD卡)上进行文件操作,但需要注意权限问题。 - **Android视图View和控件**:...

    Android 读取手机姿态并写入文本文件同时调用系统相机

    本文将深入探讨如何实现一个功能,即在用户点击按钮时,读取手机姿态并将其写入文本文件,同时在TextView中显示,然后通过另一个按钮调用系统的相机进行拍照,并将拍摄的图片保存到特定路径。 首先,让我们了解...

    android文件管理源码

    Android系统提供了丰富的API来处理文件操作,包括创建、读取、写入、删除文件等。主要涉及`java.io`和`java.nio`这两个包中的类,如File、InputStream、OutputStream、BufferedReader和BufferedWriter等。在源码中...

    android本地文件解析

    在Android开发中,本地文件解析是一项基础且重要的技能,它涉及到如何读取、写入以及处理存储在设备上的各种文件格式。"android本地文件解析"这个主题主要涵盖两个方面:txt文件解析和arrays资源文件的解析。 首先...

    Android解析并显示doc,docx,xls,xlsx文件

    在Android平台上,解析并显示Microsoft Office格式的文件(如doc, docx, xls, xlsx)是一项常见的需求,尤其在移动应用开发中。这个任务通常涉及到将这些文档转换为更易于处理的格式,例如HTML,以便在Android的...

    Android 文件存取示例 基础文件操作

    - 在此布局文件中添加一个`TextView`用于显示文件内容,以及两个`Button`分别用于打开文件和清空文件内容。 - 示例代码如下所示: ```xml &lt;AbsoluteLayout xmlns:android=...

    ANDROID文件操作

    Android文件操作主要涉及应用程序对设备存储中文件的读取和写入,这在开发移动应用时非常常见。Android系统提供了丰富的API来进行文件操作,包括使用RandomAccessFile类进行随机访问和使用字节流进行文件读写。 1. ...

    android文件选择对话框

    确保在读取和写入文件之前请求`READ_EXTERNAL_STORAGE`和`WRITE_EXTERNAL_STORAGE`权限。 8. **优化用户体验**:为了提供更好的用户体验,可以添加搜索功能,让用户快速找到所需文件,或者根据文件类型分类显示。 ...

    android网盘demo实现文件管理系统

    4. **文件操作**:Android提供了java.io和java.nio包,可以进行文件的创建、读取、写入和删除操作。对于大量文件的操作,建议使用ContentProvider,它提供了一种标准的方式来访问和管理文件,尤其是在与其他应用分享...

    可拖动TextView

    由于文件描述中提到可能有乱码问题,确保在读取或写入文本时使用正确的字符编码。在Android Studio中,项目默认的编码通常是UTF-8,如果遇到GBK编码的文本,需要进行转换。例如,在加载字符串资源时,可以使用`...

    Android TextView 复制粘贴 仿QQ空间、浏览器功能

    Android系统提供了`ClipboardManager`服务,允许应用读取和写入剪贴板数据。在TextView中添加复制功能,我们需要监听用户的长按事件,当用户长按时触发复制操作。以下是一个简单的示例: ```java textView....

    android 文件下载及进度条

    以下是一个完整的Android文件下载并显示进度条的示例,它包括了核心的下载逻辑以及UI更新。 首先,我们来看一下`AndroidDownLoadActivity`类,这是一个继承自`Activity`的类,包含了用于下载的按钮、进度条和文本...

    android:文件存取、数据库编程(中文)

    以下是一个简单的示例,演示了如何在Android应用中创建、写入、读取和显示文件内容: 1. **创建项目**:“FileWriteRead” 2. **修改布局**:在`main.xml`布局文件中,添加一个`EditText`和一个`Button`,以便用户...

    Android记事本文件

    在Android平台上开发一个记事本应用,...综上所述,开发一个Android记事本应用涵盖了UI设计、数据存储、文件操作、事件处理等多个核心知识点,需要熟练掌握Android SDK、布局设计、数据库操作以及文件系统交互等技能。

    Android学习之文件存储读取

    总结,Android的文件存储涉及到文件操作模式的选择和使用Context类提供的IO相关方法,如openFileInput()和openFileOutput(),以及文件的读写操作。理解这些概念和方法对于开发Android应用至关重要,尤其是在处理需要...

    Android持久化技术之文件的读取与写入实例详解

    - **读写文件的实现**:在`readFromFile()`和类似的方法中,使用`FileInputStream`和`BufferedReader`组合读取文件,`FileOutputStream`和`BufferedWriter`组合写入文件。`InputStreamReader`和`OutputStreamWriter...

    沙盒TextView

    3. **文件操作**:Android中的`java.io`和`android.os.File`类提供了对文件的基本操作,如创建、读取、写入和删除。在沙盒TextView的实现中,会用到这些API来处理存储在沙盒目录下的文本文件。 4. **序列化与反序列...

    安卓Android源码——仿ES文件浏览器.zip

    - 文件系统操作,如读取、写入、删除和搜索文件,以及处理目录。 - RecyclerView和Adapter的使用,这是在Android中展示列表数据的标准方式。 - 权限管理,因为访问文件系统可能需要用户授予特定权限。 - 异步操作,...

    Android编程之文件的读写实例详解

    在这个示例中,我们创建了一个垂直布局的LinearLayout,包含三个TextView(分别用于显示文件名和文件内容的提示),两个EditText(用于输入文件名和文件内容),以及两个Button(一个用于保存文件,一个用于读取文件...

Global site tag (gtag.js) - Google Analytics