-
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
分享到:
相关推荐
在Android应用程序中,读写txt文本文件是常见的需求,特别是在数据持久化或用户交互时。本文将详细讲解如何使用`context.openFileInput()`和`context.openFileOutput()`这两个方法来实现这一功能。这两个方法属于`...
- **Android文件操作**:Android提供了多种文件操作方法,包括读写文件、文件流操作等。 - **SD卡操作**:Android允许应用在外部存储(如SD卡)上进行文件操作,但需要注意权限问题。 - **Android视图View和控件**:...
本文将深入探讨如何实现一个功能,即在用户点击按钮时,读取手机姿态并将其写入文本文件,同时在TextView中显示,然后通过另一个按钮调用系统的相机进行拍照,并将拍摄的图片保存到特定路径。 首先,让我们了解...
Android系统提供了丰富的API来处理文件操作,包括创建、读取、写入、删除文件等。主要涉及`java.io`和`java.nio`这两个包中的类,如File、InputStream、OutputStream、BufferedReader和BufferedWriter等。在源码中...
在Android开发中,本地文件解析是一项基础且重要的技能,它涉及到如何读取、写入以及处理存储在设备上的各种文件格式。"android本地文件解析"这个主题主要涵盖两个方面:txt文件解析和arrays资源文件的解析。 首先...
在Android平台上,解析并显示Microsoft Office格式的文件(如doc, docx, xls, xlsx)是一项常见的需求,尤其在移动应用开发中。这个任务通常涉及到将这些文档转换为更易于处理的格式,例如HTML,以便在Android的...
- 在此布局文件中添加一个`TextView`用于显示文件内容,以及两个`Button`分别用于打开文件和清空文件内容。 - 示例代码如下所示: ```xml <AbsoluteLayout xmlns:android=...
Android文件操作主要涉及应用程序对设备存储中文件的读取和写入,这在开发移动应用时非常常见。Android系统提供了丰富的API来进行文件操作,包括使用RandomAccessFile类进行随机访问和使用字节流进行文件读写。 1. ...
确保在读取和写入文件之前请求`READ_EXTERNAL_STORAGE`和`WRITE_EXTERNAL_STORAGE`权限。 8. **优化用户体验**:为了提供更好的用户体验,可以添加搜索功能,让用户快速找到所需文件,或者根据文件类型分类显示。 ...
4. **文件操作**:Android提供了java.io和java.nio包,可以进行文件的创建、读取、写入和删除操作。对于大量文件的操作,建议使用ContentProvider,它提供了一种标准的方式来访问和管理文件,尤其是在与其他应用分享...
由于文件描述中提到可能有乱码问题,确保在读取或写入文本时使用正确的字符编码。在Android Studio中,项目默认的编码通常是UTF-8,如果遇到GBK编码的文本,需要进行转换。例如,在加载字符串资源时,可以使用`...
Android系统提供了`ClipboardManager`服务,允许应用读取和写入剪贴板数据。在TextView中添加复制功能,我们需要监听用户的长按事件,当用户长按时触发复制操作。以下是一个简单的示例: ```java textView....
以下是一个完整的Android文件下载并显示进度条的示例,它包括了核心的下载逻辑以及UI更新。 首先,我们来看一下`AndroidDownLoadActivity`类,这是一个继承自`Activity`的类,包含了用于下载的按钮、进度条和文本...
以下是一个简单的示例,演示了如何在Android应用中创建、写入、读取和显示文件内容: 1. **创建项目**:“FileWriteRead” 2. **修改布局**:在`main.xml`布局文件中,添加一个`EditText`和一个`Button`,以便用户...
在Android平台上开发一个记事本应用,...综上所述,开发一个Android记事本应用涵盖了UI设计、数据存储、文件操作、事件处理等多个核心知识点,需要熟练掌握Android SDK、布局设计、数据库操作以及文件系统交互等技能。
总结,Android的文件存储涉及到文件操作模式的选择和使用Context类提供的IO相关方法,如openFileInput()和openFileOutput(),以及文件的读写操作。理解这些概念和方法对于开发Android应用至关重要,尤其是在处理需要...
- **读写文件的实现**:在`readFromFile()`和类似的方法中,使用`FileInputStream`和`BufferedReader`组合读取文件,`FileOutputStream`和`BufferedWriter`组合写入文件。`InputStreamReader`和`OutputStreamWriter...
3. **文件操作**:Android中的`java.io`和`android.os.File`类提供了对文件的基本操作,如创建、读取、写入和删除。在沙盒TextView的实现中,会用到这些API来处理存储在沙盒目录下的文本文件。 4. **序列化与反序列...
- 文件系统操作,如读取、写入、删除和搜索文件,以及处理目录。 - RecyclerView和Adapter的使用,这是在Android中展示列表数据的标准方式。 - 权限管理,因为访问文件系统可能需要用户授予特定权限。 - 异步操作,...
在这个示例中,我们创建了一个垂直布局的LinearLayout,包含三个TextView(分别用于显示文件名和文件内容的提示),两个EditText(用于输入文件名和文件内容),以及两个Button(一个用于保存文件,一个用于读取文件...