android源代码学习文件的读取与写入以及TextView
- 格式:doc
- 大小:64.00 KB
- 文档页数:9
本文由麦可网http://www.microoh.com/ 收集整理,转载请注明出处。 android源代码学习文件的读取与写入以及TextView Java代码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 本文由麦可网http://www.microoh.com/ 收集整理,转载请注明出处。 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); } } /** 本文由麦可网http://www.microoh.com/ 收集整理,转载请注明出处。 *通过页面名称去对各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); 本文由麦可网http://www.microoh.com/ 收集整理,转载请注明出处。 } } 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