您的位置:首页 > 移动开发 > Android开发

android文件的写入与读取---简单的文本读写

2015-11-18 00:00 706 查看
摘要: android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput();

先贴上程序最后的运行效果图吧:

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



文件目录结构如下:



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工具进行查看,这里是我的一个截图:



这个项目,基本上没什么难点,就是java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流,简单、方便、快捷。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息