- packagetraining.android.com;
- importjava.io.FileNotFoundException;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.os.Handler;
- importandroid.os.Message;
- importandroid.view.KeyEvent;
- importandroid.view.Menu;
- importandroid.view.MenuItem;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.ScrollView;
- importandroid.widget.TextView;
- importandroid.widget.Toast;
- publicclassfileWriteReadextendsActivityimplementsOnClickListener{
- /**Calledwhentheactivityisfirstcreated.*/
- privateEditTextedit_Text;//编缉文本框
- privateButtonbtn_Save;//保相聚按钮
- privateStringtext_of_input;//输入文本值
- privateOutputStreamos;//输出流
- privateTextViewshowMyText;//显示文本
- privateButtonbtnOpenText,btnCleanText;//打开与清空按钮事件
- privateButtonbtnOpenRaw;
- privateStringText_of_output;//输入出文本
- privateInputStreamis;//输入流
- privateInputStreamiputRaw;//静态文件流
- privatebyte[]b;
- privateScrollViewscroll;
- privateintori=0;
- /**
- *页面的初始化
- *
- */
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- //setContentView(R.layout.main);
- showLayout("main");
- UIinit("main");
- Logic("main");
- }
- /**
- *通过页面名称去显示各页面信息创建人:周昕创建时间:2010-4-8方法名称:showLayout
- *
- *@paramlayoutName
- */
- privatevoidshowLayout(StringlayoutName){
- if(layoutName.equals("main")){
- setContentView(R.layout.main);
- }
- if(layoutName.equals("openfile")){
- setContentView(R.layout.openfile);
- }
- }
- /**
- *通过页面名称去初始化控件
- *
- *@paramuiName
- *创建人:周昕&&&&&&&&创建时间:2010-4-8
- */
- privatevoidUIinit(StringuiName){
- if(uiName.equals("main")){
- edit_Text=(EditText)findViewById(R.id.Edit_text);
- btn_Save=(Button)findViewById(R.id.Button_save);
- }
- if(uiName.equals("openfile")){
- btnOpenText=(Button)findViewById(R.id.Button_openTxt);
- btnCleanText=(Button)findViewById(R.id.Button_clean);
- btnOpenRaw=(Button)findViewById(R.id.Button_openRaw);
- showMyText=(TextView)findViewById(R.id.TextView_showTxt);
- }
- }
- /**
- *通过页面名称去对各Button指定事件
- *
- *@parampageName
- *创建人:周昕********创建时间:2010-4-8
- */
- privatevoidLogic(StringpageName){
- if(pageName.equals("main")){
- btn_Save.setOnClickListener(this);
- }
- if(pageName.equals("openfile")){
- btnOpenText.setOnClickListener(this);
- btnCleanText.setOnClickListener(this);
- btnOpenRaw.setOnClickListener(this);
- }
- }
- /**
- *在Toast中显示指定的字段
- *
- *@paramstrTitle
- */
- privatevoidNoteDebug(StringstrTitle){
- Toast.makeText(this,strTitle,Toast.LENGTH_SHORT).show();
- }
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- switch(v.getId()){
- caseR.id.Button_save:
- NoteDebug("文件保存");
- text_of_input=edit_Text.getText().toString();
- try{
- os=this.openFileOutput("txtME",MODE_PRIVATE);
- os.write(text_of_input.getBytes());
- }catch(FileNotFoundExceptione){
- NoteDebug("文件关闭失败"+e);
- }catch(IOExceptione){
- NoteDebug("文件写入失败"+e);
- }finally{
- try{
- os.close();
- }catch(IOExceptione){
- NoteDebug("关闭文件失败"+e);
- }
- }
- edit_Text.setText("");
- break;
- caseR.id.Button_openTxt:
- NoteDebug("打开文件");
- try{
- is=this.openFileInput("txtME");
- b=newbyte[1024];
- intlength=is.read(b);
- Text_of_output=newString(b);
- setTitle("文件字数"+length);
- showMyText.setText(Text_of_output);
- }catch(FileNotFoundExceptione){
- NoteDebug("文件打开失败"+e);
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- NoteDebug("文件读取失败"+e);
- }finally{
- try{
- is.close();
- }catch(IOExceptione){
- NoteDebug("文件关闭失败"+e);
- }
- }
- break;
- caseR.id.Button_openRaw:
- NoteDebug("打开静态文件");
- try{
- iputRaw=this.getResources().openRawResource(R.raw.filetext);
- b=newbyte[102400];
- intlength=iputRaw.read(b);
- Text_of_output="";
- Text_of_output=newString(b);
- setTitle("静态文件字数"+length);
- showMyText.setText(Text_of_output);
- showMyText.setHorizontallyScrolling(true);
- scroll=(ScrollView)findViewById(R.id.scroll);
- scroll.smoothScrollBy(ori,0);
- }catch(FileNotFoundExceptione){
- NoteDebug("静态文件打开失败"+e);
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- NoteDebug("静态文件读取失败"+e);
- }finally{
- try{
- iputRaw.close();
- }catch(IOExceptione){
- NoteDebug("静态文件关闭失败"+e);
- }
- }
- break;
- caseR.id.Button_clean:
- NoteDebug("清空文件");
- showMyText.setText("");
- NoteDebug("清空");
- break;
- }
- }
- publicbooleanonCreateOptionsMenu(Menumenu){
- menu.add(0,1,1,"edit");
- menu.add(0,2,2,"open");
- menu.add(0,3,3,"clear");
- returnsuper.onCreateOptionsMenu(menu);
- }
- publicbooleanonOptionsItemSelected(MenuItemitems){
- switch(items.getItemId()){
- case1:
- showLayout("main");
- UIinit("main");
- Logic("main");
- NoteDebug("编缉文件");
- break;
- case2:
- showLayout("openfile");
- UIinit("openfile");
- Logic("openfile");
- NoteDebug("打开件");
- break;
- case3:
- showLayout("openfile");
- UIinit("openfile");
- Logic("openfile");
- NoteDebug("清空文件");
- break;
- }
- returnsuper.onOptionsItemSelected(items);
- }
- privateHandlermessage=newHandler(){
- publicvoidhandleMessage(Messagemsg){
- doScrow();
- }
- };
- publicclassTimerLoopimplementsRunnable{
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- while(true){
- loop(500);
- message.sendEmptyMessage(0);
- }
- }
- }
- //因为sleep()似乎不好用所以采用这种方法计时
- publicvoidloop(longi){
- longj=i;
- while(j>0){
- j=j-1;
- }
- }
- publicbooleanonKeyDown(intkeyCode,KeyEventmsg){
- //Threadloop=newThread(newTimerLoop());
- //loop.start();
- returnsuper.onKeyDown(keyCode,msg);
- }
- publicvoiddoScrow(){
- intnow=scroll.getScrollY();
- if(ori==now){
- scroll.scrollTo(now,0);
- ori=-1;
- }
- else{
- scroll.smoothScrollBy(10,10);
- ori=now;
- }
- }
- }
main.xml文件
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:id="@+id/Edit_text"
- android:layout_width="fill_parent"
- android:layout_height="350px">
- </EditText>
- <Button
- android:text="保存"
- android:id="@+id/Button_save"
- android:layout_width="80px"
- android:layout_height="wrap_content">
- </Button>
- </LinearLayout>
open.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <AbsoluteLayoutandroid:id="@+id/openlayout"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <!--<ScrollViewandroid:layout_weight="1"android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:scrollbars="vertical"
- android:fadingEdge="vertical"android:id="@+id/scroll">
- -->
- <ScrollView
- android:id="@+id/scroll"
- android:layout_width="fill_parent"
- android:layout_height="300dip">
- <LinearLayoutandroid:layout_width="fill_parent"
- android:layout_height="380px">
- <TextViewandroid:id="@+id/TextView_showTxt"
- android:layout_width="314px"android:layout_height="373px"
- android:scrollbars="vertical"android:fadingEdge="vertical"
- android:autoText="true"android:layout_x="3px"android:layout_y="3px"
- >
- <!--android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"-->
- </TextView>
- </LinearLayout>
- </ScrollView>
- <Buttonandroid:id="@+id/Button_openTxt"android:layout_width="80px"
- android:layout_height="wrap_content"android:text="打开"
- android:layout_x="2px"android:layout_y="378px">
- </Button>
- <Buttonandroid:id="@+id/Button_openRaw"android:layout_width="80px"
- android:layout_height="wrap_content"android:text="静态文件"
- android:layout_x="102px"android:layout_y="378px">
- </Button>
- <Buttonandroid:id="@+id/Button_clean"android:layout_width="80px"
- android:layout_height="wrap_content"android:text="清空"
- android:layout_x="239px"android:layout_y="378px">
- </Button>
- </AbsoluteLayout>
AndroidMainfest.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="training.android.com"android:versionCode="1"
- android:versionName="1.0">
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <activityandroid:name=".fileWriteRead"android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- <uses-sdkandroid:minSdkVersion="3"/>
- <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <uses-permissionandroid:name="android.permission.MODE_WORLD_READABLE"></uses-permission>
- <uses-permissionandroid:name="android.permission.MODE_WORLD_WRITEABLE"></uses-permission>
- </manifest>
http://www.cnblogs.com/maoan/archive/2011/01/18/1938278.html