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

  1. package training.android.com;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.os.Handler;  
  10. import android.os.Message;  
  11. import android.view.KeyEvent;  
  12. import android.view.Menu;  
  13. import android.view.MenuItem;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17. import android.widget.EditText;  
  18. import android.widget.ScrollView;  
  19. import android.widget.TextView;  
  20. import android.widget.Toast;  
  21.   
  22. public class fileWriteRead extends Activity implements OnClickListener {  
  23.     /** Called when the activity is first created. */  
  24.     private EditText edit_Text;// 编缉文本框  
  25.     private Button btn_Save;// 保相聚按钮  
  26.     private String text_of_input;// 输入文本值  
  27.     private OutputStream os;// 输出流  
  28.     private TextView showMyText;// 显示文本  
  29.     private Button btnOpenText, btnCleanText;// 打开与清空按钮事件  
  30.     private Button btnOpenRaw;  
  31.     private String Text_of_output;// 输入出文本  
  32.     private InputStream is;// 输入流  
  33.     private InputStream iputRaw;//静态文件流  
  34.     private byte[] b;  
  35.     private ScrollView scroll;  
  36.     private int ori=0;  
  37.     /** 
  38.      * 页面的初始化 
  39.      *  
  40.      */  
  41.     @Override  
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         // setContentView(R.layout.main);  
  45.         showLayout("main");  
  46.         UIinit("main");  
  47.         Logic("main");  
  48.     }  
  49.   
  50.     /** 
  51.      * 通过页面名称去显示各页面信息 创建人:周昕 创建时间:2010-4-8 方法名称:showLayout 
  52.      *  
  53.      * @param layoutName 
  54.      */  
  55.     private void showLayout(String layoutName) {  
  56.         if (layoutName.equals("main")) {  
  57.             setContentView(R.layout.main);  
  58.         }  
  59.         if (layoutName.equals("openfile")) {  
  60.             setContentView(R.layout.openfile);  
  61.         }  
  62.   
  63.     }  
  64.   
  65.     /** 
  66.      * 通过页面名称去初始化控件 
  67.      *  
  68.      * @param uiName 
  69.      *            创建人:周昕 &&&&&&&& 创建时间:2010-4-8 
  70.      */  
  71.     private void UIinit(String uiName) {  
  72.         if (uiName.equals("main")) {  
  73.             edit_Text = (EditText) findViewById(R.id.Edit_text);  
  74.             btn_Save = (Button) findViewById(R.id.Button_save);  
  75.         }  
  76.         if (uiName.equals("openfile")) {  
  77.             btnOpenText = (Button) findViewById(R.id.Button_openTxt);  
  78.             btnCleanText = (Button) findViewById(R.id.Button_clean);  
  79.             btnOpenRaw = (Button) findViewById(R.id.Button_openRaw);  
  80.             showMyText = (TextView) findViewById(R.id.TextView_showTxt);  
  81.         }  
  82.     }  
  83.   
  84.     /** 
  85.      * 通过页面名称去对各Button指定事件 
  86.      *  
  87.      * @param pageName 
  88.      *            创建人:周昕******** 创建时间:2010-4-8 
  89.      */  
  90.     private void Logic(String pageName) {  
  91.         if (pageName.equals("main")) {  
  92.             btn_Save.setOnClickListener(this);  
  93.         }  
  94.         if (pageName.equals("openfile")) {  
  95.             btnOpenText.setOnClickListener(this);  
  96.             btnCleanText.setOnClickListener(this);  
  97.             btnOpenRaw.setOnClickListener(this);  
  98.         }  
  99.     }  
  100.   
  101.     /** 
  102.      * 在Toast中显示指定的字段 
  103.      *  
  104.      * @param strTitle 
  105.      */  
  106.     private void NoteDebug(String strTitle) {  
  107.         Toast.makeText(this, strTitle, Toast.LENGTH_SHORT).show();  
  108.     }  
  109.   
  110.     @Override  
  111.     public void onClick(View v) {  
  112.         // TODO Auto-generated method stub  
  113.         switch (v.getId()) {  
  114.         case R.id.Button_save:  
  115.             NoteDebug("文件保存");  
  116.             text_of_input = edit_Text.getText().toString();  
  117.             try {  
  118.                 os = this.openFileOutput("txtME", MODE_PRIVATE);  
  119.                 os.write(text_of_input.getBytes());  
  120.             } catch (FileNotFoundException e) {  
  121.                 NoteDebug("文件关闭失败" + e);  
  122.             } catch (IOException e) {  
  123.                 NoteDebug("文件写入失败" + e);  
  124.             } finally {  
  125.                 try {  
  126.                     os.close();  
  127.                 } catch (IOException e) {  
  128.                     NoteDebug("关闭文件失败" + e);  
  129.                 }  
  130.             }  
  131.             edit_Text.setText("");  
  132.             break;  
  133.         case R.id.Button_openTxt:  
  134.             NoteDebug("打开文件");  
  135.             try {  
  136.                 is = this.openFileInput("txtME");  
  137.                 b = new byte[1024];  
  138.                 int length = is.read(b);  
  139.                 Text_of_output = new String(b);  
  140.                 setTitle("文件字数" + length);  
  141.                 showMyText.setText(Text_of_output);  
  142.             } catch (FileNotFoundException e) {  
  143.                 NoteDebug("文件打开失败" + e);  
  144.             } catch (IOException e) {  
  145.                 // TODO Auto-generated catch block  
  146.                 NoteDebug("文件读取失败" + e);  
  147.             } finally {  
  148.                 try {  
  149.                     is.close();  
  150.                 } catch (IOException e) {  
  151.                     NoteDebug("文件关闭失败" + e);  
  152.                 }  
  153.             }  
  154.             break;  
  155.         case R.id.Button_openRaw:  
  156.             NoteDebug("打开静态文件");  
  157.             try {  
  158.                 iputRaw = this.getResources().openRawResource(R.raw.filetext);  
  159.                 b = new byte[102400];  
  160.                 int length = iputRaw.read(b);  
  161.                 Text_of_output="";  
  162.                 Text_of_output = new String(b);  
  163.                 setTitle("静态文件字数" + length);  
  164.                 showMyText.setText(Text_of_output);  
  165.                 showMyText.setHorizontallyScrolling(true);  
  166.                 scroll = (ScrollView)findViewById(R.id.scroll);      
  167.                 scroll.smoothScrollBy(ori, 0);  
  168.   
  169.             } catch (FileNotFoundException e) {  
  170.                 NoteDebug("静态文件打开失败" + e);  
  171.             } catch (IOException e) {  
  172.                 // TODO Auto-generated catch block  
  173.                 NoteDebug("静态文件读取失败" + e);  
  174.             } finally {  
  175.                 try {  
  176.                     iputRaw.close();  
  177.                 } catch (IOException e) {  
  178.                     NoteDebug("静态文件关闭失败" + e);  
  179.                 }  
  180.             }  
  181.             break;  
  182.         case R.id.Button_clean:  
  183.             NoteDebug("清空文件");  
  184.             showMyText.setText("");  
  185.             NoteDebug("清空");  
  186.             break;  
  187.         }  
  188.   
  189.     }  
  190.   
  191.     public boolean onCreateOptionsMenu(Menu menu) {  
  192.         menu.add(0, 1, 1, "edit");  
  193.         menu.add(0, 2, 2, "open");  
  194.         menu.add(0, 3, 3, "clear");  
  195.         return super.onCreateOptionsMenu(menu);  
  196.     }  
  197.   
  198.     public boolean onOptionsItemSelected(MenuItem items) {  
  199.         switch (items.getItemId()) {  
  200.         case 1:  
  201.             showLayout("main");  
  202.             UIinit("main");  
  203.             Logic("main");  
  204.             NoteDebug("编缉文件");  
  205.             break;  
  206.         case 2:  
  207.             showLayout("openfile");  
  208.             UIinit("openfile");  
  209.             Logic("openfile");  
  210.             NoteDebug("打开件");  
  211.             break;  
  212.         case 3:  
  213.             showLayout("openfile");  
  214.             UIinit("openfile");  
  215.             Logic("openfile");  
  216.             NoteDebug("清空文件");  
  217.             break;  
  218.         }  
  219.         return super.onOptionsItemSelected(items);  
  220.     }  
  221.       
  222.      private Handler message = new Handler(){  
  223.             public void handleMessage(Message msg) {  
  224.                 doScrow();  
  225.                       
  226.                 }  
  227.         };  
  228.               
  229.         public class TimerLoop implements Runnable {  
  230.             @Override  
  231.             public void run() {  
  232.                 // TODO Auto-generated method stub  
  233.                   
  234.                 while(true){  
  235.                     loop(500);  
  236.                       
  237.                     message.sendEmptyMessage(0);  
  238.                 }  
  239.             }  
  240.               
  241.         }  
  242.           
  243.     //因为sleep()似乎不好用 所以采用这种方法计时  
  244.         public void loop(long i){  
  245.             long j = i;  
  246.             while(j>0){  
  247.                   
  248.                 j = j-1;  
  249.             }  
  250.   
  251.         }  
  252.         public boolean onKeyDown(int keyCode, KeyEvent msg){  
  253. //          Thread loop = new Thread(new TimerLoop());  
  254. //          loop.start();  
  255.               
  256.             return super.onKeyDown(keyCode, msg);  
  257.         }  
  258.         public void doScrow(){  
  259.             int now = scroll.getScrollY();  
  260.               
  261.             if(ori == now){  
  262.                 scroll.scrollTo(now, 0);  
  263.                 ori = -1;  
  264.                   
  265.             }  
  266.             else {  
  267.                 scroll.smoothScrollBy(10, 10);  
  268.                   
  269.                 ori = now;  
  270.                   
  271.             }  
  272.         }  
  273. }

main.xml文件

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

open.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <AbsoluteLayout android: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.     <!-- <ScrollView android: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.         <LinearLayout android:layout_width="fill_parent"  
  14.             android:layout_height="380px">  
  15.             <TextView android:id="@+id/TextView_showTxt"  
  16.                 android:layout_width="314px" android:layout_height="373px"  
  17.                   android:scrollbars="vertical" android:fadingEdge="vertical"  
  18.                   
  19.                 android:autoText="true" android:layout_x="3px" android:layout_y="3px"  
  20.                 >  
  21.                 <!-- android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" -->  
  22.             </TextView>  
  23.         </LinearLayout>  
  24.     </ScrollView>  
  25.       
  26.     <Button android:id="@+id/Button_openTxt" android:layout_width="80px"  
  27.         android:layout_height="wrap_content" android:text="打开"  
  28.         android:layout_x="2px" android:layout_y="378px">  
  29.     </Button>  
  30.     <Button android:id="@+id/Button_openRaw" android:layout_width="80px"  
  31.         android:layout_height="wrap_content" android:text="静态文件"  
  32.         android:layout_x="102px" android:layout_y="378px">  
  33.     </Button>  
  34.     <Button android:id="@+id/Button_clean" android:layout_width="80px"  
  35.         android:layout_height="wrap_content" android:text="清空"  
  36.         android:layout_x="239px" android:layout_y="378px">  
  37.     </Button>  
  38. </AbsoluteLayout>

AndroidMainfest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="training.android.com" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  6.         <activity android:name=".fileWriteRead" android:label="@string/app_name">  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.MAIN" />  
  9.                 <category android:name="android.intent.category.LAUNCHER" />  
  10.             </intent-filter>  
  11.         </activity>  
  12.   
  13.     </application>  
  14.     <uses-sdk android:minSdkVersion="3" />  
  15.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
  16.     <uses-permission android:name="android.permission.MODE_WORLD_READABLE"></uses-permission>  
  17.     <uses-permission android:name="android.permission.MODE_WORLD_WRITEABLE"></uses-permission>  
  18. </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、付费专栏及课程。

余额充值