最终效果图,点击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为我们提供了两个方法来获取输入输出流。简单、方便、快捷啊。

文章转载,版权归原作者所有,尊重版权,支持原创

【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()的更多相关文章

  1. android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()

      最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding=" ...

  2. Android 下载文件及写入SD卡

    Android 下载文件及写入SD卡,实例代码 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  3. INI文件的写入与读取

    INI文件的写入与读取 [节名]         '[]中的节名对应此API的第一参数 Name=内容      'Nmae对应此API的第二参数 API的第三参数是没有取到匹配内容时返回的字符串; ...

  4. 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取

    装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...

  5. JavaIO流——简单对文件的写入及读取(三)

    已经讲了写入和读取了,那么想要把一个文件的内容复制到另一个文件呢 不说太多,直接见代码 public static void copyFile(String srcFilename, String d ...

  6. java 文件操作 写入和读取(小结一)

    参考了这篇博客并优化,谢谢:http://blog.sina.com.cn/s/blog_99201d890101b4le.html 功能:  实现通过两个类完成先写入文件,再读取数据计算显示 pac ...

  7. [转]VC++中对文件的写入和读取

    本文转自:http://blog.csdn.net/fanghb_1984/article/details/7425705 本文介绍两种方法对文件进行读取和写入操作:1.采用fstream类:2.采用 ...

  8. 第十七章,txt文件的写入和读取数据结合练习(C++)

    #include <iostream> #include <fstream> int main(int argc, char** argv) { std::string str ...

  9. Java file文件的写入和读取及下载

    File文件的写入 一.FileWriter 和BufferedWriter 结合写入文件 FileWriter是字符流写入字符到文件.默认情况下,它会使用新的内容代替文件原有的所有内容,但是,当指定 ...

随机推荐

  1. python连接kafka生产者,消费者脚本

    # -*- coding: utf-8 -*- ''''' 使用kafka-Python 1.3.3模块 # pip install kafka==1.3.5 # pip install kafka- ...

  2. var_export 掉咋天

    var_export     文件缓存经常使用    输出或返回一个变量的字符串表示 /** * 写入缓存 * * @param string $id * @param mixed $data * @ ...

  3. inode索引详解

    理解inode inode是一个重要概念,是理解Unix/Linux文件系统和硬盘储存的基础. 我觉得,理解inode,不仅有助于提高系统操作水平,还有助于体会Unix设计哲学,即如何把底层的复杂性抽 ...

  4. nginx 配置白名单

    在http 模块 增加 geo $remote_addr $ip_whitelist{ default 0; include white_ip.conf; } 在location 模块 增加 (注意i ...

  5. Confluence 6 基本性能问题诊断步骤

    基本性能问题诊断步骤 开始下面的程序: 进入 Troubleshooting Confluence hanging or crashing页面找到已知的主要性能问题. 进行页面 Performance ...

  6. Python基础之类方法和静态方法

    小叙一会儿: 通常情况下,在类中定义的所有函数(注意了,这里说的就是所有,跟self啥的没关系,self也只是一个再普通不过 的参数而已)都是对象的绑定方法,对象在调用绑定方法时会自动将自己作为参数传 ...

  7. Solver Of Caffe

    本文旨在解决如何编写solver文件. Solver的流程: 1.     设计好需要优化的对象,以及用于学习的训练网络和用于评估的测试网络.(通过调用另外一个配置文件prototxt来进行) 2.  ...

  8. python网络爬虫笔记(二)

    一.函数调用的默认设置 1.def enroll(name,grnder,age=4,city='Shanghai'): print (''name:',name) print (''gender', ...

  9. BrupSuite渗透测试笔记(九)

    一. Update BurpSuite 1.选择help ,点击check for updates 记可以进入最新版本的下载界面,profession version need pay for mon ...

  10. C++ 关于ShowWindow()的疑问

    IDE: Code::Blocks 16.01 操作系统:Windows 7 x64 最初的代码,目的是为了隐藏窗口出现在任务栏上的图标. #include <windows.h> usi ...