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

本文介绍了一个简单的Android应用程序示例,演示了如何在Android设备上进行文件的读写操作,包括保存文本到本地文件、从本地文件读取文本、清空文件内容以及读取资源中的静态文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package training.android.com; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; public class fileWriteRead extends Activity implements OnClickListener { /** Called when the activity is first created. */ private EditText edit_Text;// 编缉文本框 private Button btn_Save;// 保相聚按钮 private String text_of_input;// 输入文本值 private OutputStream os;// 输出流 private TextView showMyText;// 显示文本 private Button btnOpenText, btnCleanText;// 打开与清空按钮事件 private Button btnOpenRaw; private String Text_of_output;// 输入出文本 private InputStream is;// 输入流 private InputStream iputRaw;//静态文件流 private byte[] b; private ScrollView scroll; private int ori=0; /** * 页面的初始化 * */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.main); showLayout("main"); UIinit("main"); Logic("main"); } /** * 通过页面名称去显示各页面信息 创建人:周昕 创建时间:2010-4-8 方法名称:showLayout * * @param layoutName */ private void showLayout(String layoutName) { if (layoutName.equals("main")) { setContentView(R.layout.main); } if (layoutName.equals("openfile")) { setContentView(R.layout.openfile); } } /** * 通过页面名称去初始化控件 * * @param uiName * 创建人:周昕 &&&&&&&& 创建时间:2010-4-8 */ private void UIinit(String uiName) { 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指定事件 * * @param pageName * 创建人:周昕******** 创建时间:2010-4-8 */ private void Logic(String pageName) { if (pageName.equals("main")) { btn_Save.setOnClickListener(this); } if (pageName.equals("openfile")) { btnOpenText.setOnClickListener(this); btnCleanText.setOnClickListener(this); btnOpenRaw.setOnClickListener(this); } } /** * 在Toast中显示指定的字段 * * @param strTitle */ private void NoteDebug(String strTitle) { Toast.makeText(this, strTitle, Toast.LENGTH_SHORT).show(); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.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 (FileNotFoundException e) { NoteDebug("文件关闭失败" + e); } catch (IOException e) { NoteDebug("文件写入失败" + e); } finally { try { os.close(); } catch (IOException e) { NoteDebug("关闭文件失败" + e); } } edit_Text.setText(""); break; case R.id.Button_openTxt: NoteDebug("打开文件"); try { is = this.openFileInput("txtME"); b = new byte[1024]; int length = is.read(b); Text_of_output = new String(b); setTitle("文件字数" + length); showMyText.setText(Text_of_output); } catch (FileNotFoundException e) { NoteDebug("文件打开失败" + e); } catch (IOException e) { // TODO Auto-generated catch block NoteDebug("文件读取失败" + e); } finally { try { is.close(); } catch (IOException e) { NoteDebug("文件关闭失败" + e); } } break; case R.id.Button_openRaw: NoteDebug("打开静态文件"); try { iputRaw = this.getResources().openRawResource(R.raw.filetext); b = new byte[102400]; int length = iputRaw.read(b); Text_of_output=""; Text_of_output = new String(b); setTitle("静态文件字数" + length); showMyText.setText(Text_of_output); showMyText.setHorizontallyScrolling(true); scroll = (ScrollView)findViewById(R.id.scroll); scroll.smoothScrollBy(ori, 0); } catch (FileNotFoundException e) { NoteDebug("静态文件打开失败" + e); } catch (IOException e) { // TODO Auto-generated catch block NoteDebug("静态文件读取失败" + e); } finally { try { iputRaw.close(); } catch (IOException e) { NoteDebug("静态文件关闭失败" + e); } } break; case R.id.Button_clean: NoteDebug("清空文件"); showMyText.setText(""); NoteDebug("清空"); break; } } public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 1, 1, "edit"); menu.add(0, 2, 2, "open"); menu.add(0, 3, 3, "clear"); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem items) { switch (items.getItemId()) { case 1: showLayout("main"); UIinit("main"); Logic("main"); NoteDebug("编缉文件"); break; case 2: showLayout("openfile"); UIinit("openfile"); Logic("openfile"); NoteDebug("打开件"); break; case 3: showLayout("openfile"); UIinit("openfile"); Logic("openfile"); NoteDebug("清空文件"); break; } return super.onOptionsItemSelected(items); } private Handler message = new Handler(){ public void handleMessage(Message msg) { doScrow(); } }; public class TimerLoop implements Runnable { @Override public void run() { // TODO Auto-generated method stub while(true){ loop(500); message.sendEmptyMessage(0); } } } //因为sleep()似乎不好用 所以采用这种方法计时 public void loop(long i){ long j = i; while(j>0){ j = j-1; } } public boolean onKeyDown(int keyCode, KeyEvent msg){ // Thread loop = new Thread(new TimerLoop()); // loop.start(); return super.onKeyDown(keyCode, msg); } public void doScrow(){ int now = scroll.getScrollY(); if(ori == now){ scroll.scrollTo(now, 0); ori = -1; } else { scroll.smoothScrollBy(10, 10); ori = now; } } }

main.xml文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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

<?xml version="1.0" encoding="UTF-8"?> <AbsoluteLayout android:id="@+id/openlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- <ScrollView android: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" > <LinearLayout android:layout_width="fill_parent" android:layout_height="380px"> <TextView android: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> <Button android:id="@+id/Button_openTxt" android:layout_width="80px" android:layout_height="wrap_content" android:text="打开" android:layout_x="2px" android:layout_y="378px"> </Button> <Button android:id="@+id/Button_openRaw" android:layout_width="80px" android:layout_height="wrap_content" android:text="静态文件" android:layout_x="102px" android:layout_y="378px"> </Button> <Button android: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

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="training.android.com" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".fileWriteRead" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.MODE_WORLD_READABLE"></uses-permission> <uses-permission android:name="android.permission.MODE_WORLD_WRITEABLE"></uses-permission> </manifest>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值