android文件的写入与读取---简单的文本读写
2015-11-18 00:00
706 查看
摘要: android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput();
先贴上程序最后的运行效果图吧:
点击save会保存到文件中,点击show会从文件中读取出内容并显示。
![](http://static.oschina.net/uploads/space/2015/1118/102331_6o3g_2391602.png)
文件目录结构如下:
![](http://static.oschina.net/uploads/space/2015/1118/103119_D2IR_2391602.png)
main_activity.xml文件:
Activity.java文件如下:
然而项目中的其他文件可以默认也是可以自己美化吧,这里就随意了;
关于文件保存的路径可以通过AndroidStudio携带的File Explorer工具进行查看,这里是我的一个截图:
![](http://static.oschina.net/uploads/space/2015/1118/104036_x4rv_2391602.png)
这个项目,基本上没什么难点,就是java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流,简单、方便、快捷。
先贴上程序最后的运行效果图吧:
点击save会保存到文件中,点击show会从文件中读取出内容并显示。
![](http://static.oschina.net/uploads/space/2015/1118/102331_6o3g_2391602.png)
文件目录结构如下:
![](http://static.oschina.net/uploads/space/2015/1118/103119_D2IR_2391602.png)
main_activity.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入你要保存的内容:" android:textColor="@color/colorPrimaryDark" android:textSize="15sp" /> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请在此处输入内容!" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/save" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="save" /> <Button android:id="@+id/show" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="show" /> </LinearLayout> <TextView android:id="@+id/showTextView" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@color/colorAccent" android:textSize="15sp" /> </LinearLayout>
Activity.java文件如下:
package com.yaowen.androidfilewriteandread; import android.app.Activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView showText;//显示保存内容的控件 private Button save, show;//保存和显示按钮 private EditText saveText;//保存内容的输入框 private String fileName = "test.txt";//保存的文件名 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); saveText = (EditText) findViewById(R.id.editText); save = (Button) findViewById(R.id.save); show = (Button) findViewById(R.id.show); showText = (TextView) findViewById(R.id.showTextView); //设置按钮的点击事件 save.setOnClickListener(this); show.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.save: { saveTextView();//保存文件 break; } case R.id.show: { showTextView();//读取文件并显示文件内容 break; } } } private void showTextView() { try { /* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的, * public abstract FileOutputStream openFileOutput(String name, int mode) * throws FileNotFoundException; * openFileOutput(String name, int mode); * 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名 * 该文件会被保存在/data/data/应用名称/files/test.txt * 第二个参数,代表文件的操作模式 * MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖 * MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 * MODE_WORLD_READABLE 公用 可读 * MODE_WORLD_WRITEABLE 公用 可读写 * */ 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()); if (content != null && content.length() > 0) {//这里做了个判断,判断content要有内容 showText.setText(content); } else { Toast.makeText(MainActivity.this, "文件内容为空,请修改或者添加内容!", Toast.LENGTH_SHORT).show(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void saveTextView() { String content = saveText.getText().toString(); if (content != null && content.length() > 0) {//这里做了个判断,判断content要有内容 try { FileOutputStream outputStream = openFileOutput(fileName, Activity.MODE_PRIVATE); outputStream.write(content.getBytes()); outputStream.flush(); outputStream.close(); Toast.makeText(MainActivity.this, "保存内容成功!", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(MainActivity.this, "请输入在输入框里输入要保存的内容!", Toast.LENGTH_SHORT).show(); } } }
然而项目中的其他文件可以默认也是可以自己美化吧,这里就随意了;
关于文件保存的路径可以通过AndroidStudio携带的File Explorer工具进行查看,这里是我的一个截图:
![](http://static.oschina.net/uploads/space/2015/1118/104036_x4rv_2391602.png)
这个项目,基本上没什么难点,就是java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流,简单、方便、快捷。
相关文章推荐
- sql2005 附加数据库出错(错误号:5123)解决方法
- 请问在mssql“SQL事件探查器”里表格的标题,如CPU,Read,Write,Duration,SPID.........的解释
- C#中File类的文件操作方法详解
- C++文件读写代码分享
- C语言中对文件最基本的读取和写入函数
- asp #include file 与 #include virtual 的区别小结第1/2页
- PHP file_exists问题杂谈
- Stream.Write 与 StreamWriter.Write 的不同
- css美化input file按钮的代码方法
- 在网页中使用document.write时遭遇的奇怪问题
- 改变文件域的样式实现思路同时兼容ie、firefox
- js中document.write使用过程中的一点疑问解答
- How to Auto Include a Javascript File
- jQuery中的read和JavaScript中的onload函数的区别
- 清理SQL Server 2008日志文件Cannot shrink log file 2 的解决方案
- Locate a File Using a File Open Dialog Box
- Save a File Using a File Save Dialog Box
- 基于java file 文件操作operate file of java的应用
- php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
- IO流介绍与File类