Android应用程序中读写txt文本文件的基本方法讲解

最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示。

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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请您输入要保存的内容:" /> <EditText android:id="@+id/addText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请您在此处输入文件内容!" /> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="save" /> <Button android:id="@+id/showButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="show" /> <TextView android:id="@+id/showText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

activity代码

package cn.com.file; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class FileTest extends Activity { private EditText editText; private TextView showTextView; // 要保存的文件名 private String fileName = "chenzheng_java.txt"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取页面中的组件 editText = (EditText) findViewById(R.id.addText); showTextView = (TextView) findViewById(R.id.showText); Button addButton = (Button) this.findViewById(R.id.addButton); Button showButton = (Button) this.findViewById(R.id.showButton); // 绑定单击事件 addButton.setOnClickListener(listener); showButton.setOnClickListener(listener); } // 声明监听器 private View.OnClickListener listener = new OnClickListener() { public void onClick(View v) { Button view = (Button) v; switch (view.getId()) { case R.id.addButton: save(); break; case R.id.showButton: read(); break; } } }; /** *@author chenzheng_Java *保存用户输入的内容到文件 */ private void save() { String content = editText.getText().toString(); try { /* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的, * 至于为什么这个地方还有FileNotFoundException抛出,我也比较纳闷。在Context中是这样定义的 * public abstract FileOutputStream openFileOutput(String name, int mode) * throws FileNotFoundException; * openFileOutput(String name, int mode); * 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名 * 该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt * 第二个参数,代表文件的操作模式 * MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖 * MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 * MODE_WORLD_READABLE 公用 可读 * MODE_WORLD_WRITEABLE 公用 可读写 * */ FileOutputStream outputStream = openFileOutput(fileName, Activity.MODE_PRIVATE); outputStream.write(content.getBytes()); outputStream.flush(); outputStream.close(); Toast.makeText(FileTest.this, "保存成功", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * @author chenzheng_java * 读取刚才用户保存的内容 */ private void read() { try { FileInputStream inputStream = this.openFileInput(fileName); byte[] bytes = new byte[1024]; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); while (inputStream.read(bytes) != -1) { arrayOutputStream.write(bytes, 0, bytes.length); } inputStream.close(); arrayOutputStream.close(); String content = new String(arrayOutputStream.toByteArray()); showTextView.setText(content); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

其他的都为默认。

关于文件保存的路径可以通过ADT携带的File Explorer工具进行查看。如何调出File Explorer工具呢;我们可以通过Windows--showView--others-android下面看到File Explorer。这里是我的一个截图。

对于这个程序,基本上没什么难点,就是纯粹的java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流。简单、方便、快捷啊。

时间: 2024-08-01 19:27:24

Android应用程序中读写txt文本文件的基本方法讲解的相关文章

Android应用程序中读写txt文本文件的基本方法讲解_Android

最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layou

在Android应用程序中使用Internet数据

常用缩略词 Ajax: Asynchronous JavaScript + XML API:应用程序编程接口 CSV:逗号分隔值 CSS:层叠样式表 DOM:文档对象模型 HTML:超文本标记语言 HTTP:超文本传输协议 IDL:接口 描述语言 JSON:Javascript 对象标识 SAX:XML 简单 API SDK:软件开发包 UI:用户界面 URL:统一资源定位符 XML:可扩展标记语言 3G:第三代手机技术标准 Android 应用程序必须访问位于 Internet 上的数据,而

广告-在android应用程序中启动其他apk程序,被启动程序退出后返回之前的程序

问题描述 在android应用程序中启动其他apk程序,被启动程序退出后返回之前的程序 大家好,我现在遇到这样的情况,我目前做的是一个android积分墙的项目,用户通过我的这个项目app做任务下载一个广告,当用户进入到我们的下载广告的apk 后,玩了一段时间后(玩了一段时间才会给积分),点击后退,当前的 广告apk 是退出了,同时我们的项目也退出了(原本我们的项目逻辑是不会退出的),回到了桌面......我们自己测试的时候并没有出现这样的情况,但是用户遇到了,并且用户反馈再次重启手机的时候,再

从android应用程序中连接网站

问题描述 从android应用程序中连接网站 我开发一个简单的android程序,当点击程序主页的"开始"按钮时,重新定向到一个指定的网站(比如google).我设置了开始按钮的 @id 是 'bStart'. 要在下面的方法中加什么代码呢? start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method

android在程序中调用另一个apk行不行?

问题描述 android在程序中调用另一个apk行不行? android在程序中调用另一个apk行不行?如果行,请问怎么实现? 解决方案 参考:http://www.cnblogs.com/winxiang/archive/2012/05/04/2482883.html 解决方案二: http://www.cnblogs.com/winxiang/archive/2012/05/04/2482883.html 解决方案三: 可以,看你想实现什么功能了. 解决方案四: 这个很容易 利用Intent

点击按钮时android应用程序中无响应

问题描述 点击按钮在android应用程序中无响应 解决方案 解决方案二:联机调试啊

在Node.js应用中读写Redis数据库的简单方法

  这篇文章主要介绍了在Node.js应用中读写Redis数据库的简单方法,Redis是一个内存式高速数据库,需要的朋友可以参考下 在开始本文之前请确保安装好 Redis 和 Node.js 以及 Node.js 的 Redis 扩展 -- node_redis 首先创建一个新文件夹并新建文本文件 app.js 文件内容如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var redis = require("redis") ,

在.net中读写config文件的各种方法

原文 http://www.cnblogs.com/fish-li/archive/2011/12/18/2292037.html 阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collection config文件 - 读与写 读写 .net framework中已经定义的节点 xml配置文件 xml配置文件 - CDATA xml文件读写注意事项 配置

详解在.net中读写config文件的各种方法_实用技巧

今天谈谈在.net中读写config文件的各种方法. 在这篇博客中,我将介绍各种配置文件的读写操作. 由于内容较为直观,因此没有过多的空道理,只有实实在在的演示代码, 目的只为了再现实战开发中的各种场景.希望大家能喜欢. 通常,我们在.NET开发过程中,会接触二种类型的配置文件:config文件,xml文件. 今天的博客示例也将介绍这二大类的配置文件的各类操作. 在config文件中,我将主要演示如何创建自己的自定义的配置节点,而不是介绍如何使用appSetting . 请明:本文所说的conf