利用Context类中的openFIleOutput方法可以将数据存储到指定的文件中
openFileOutput方法接收两个参数:
第一个为文件的名称,例如“data”,不可以包含路径,因为所有的文件都是默认存储到/data/data/<package name>/files/目录下
第二个参数为要进行的操作,有两种模式:
1.Context.MODE_PRIVATE 每次都创建新的文件然后保存进去
2. Context.MODE_APPEND 若文件不存在则创建文件,若文件存在则在文件中追加内容
下面是示例代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/save"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/save_btn"
android:text="存储"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/save_append_btn"
android:text="追加存储"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/read_btn"
android:text="读取"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/read_content"/>
</LinearLayout>
MainActivity.java
package ble.ysu.com.savedemo;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText editText;
private Button saveBtn;
private Button readBtn;
private TextView readContent;
private Button saveAppendBtn;
public static final int CREATE = 1;
public static final int APPEND = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
editText = findViewById(R.id.save);
saveBtn = findViewById(R.id.save_btn);
readBtn = findViewById(R.id.read_btn);
readContent = findViewById(R.id.read_content);
saveAppendBtn = findViewById(R.id.save_append_btn);
editText.setOnClickListener(this);
saveBtn.setOnClickListener(this);
readBtn.setOnClickListener(this);
readContent.setOnClickListener(this);
saveAppendBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.save_btn:
save(editText.getText().toString(),CREATE);
break;
case R.id.save_append_btn:
save(editText.getText().toString(),APPEND);
break;
case R.id.read_btn:
read();
break;
default:
break;
}
}
private void save(String data , int mode){
FileOutputStream out = null;
BufferedWriter writer = null;
try{
if(mode == CREATE){
out = openFileOutput("data",Context.MODE_PRIVATE);
}else{
out = openFileOutput("data",Context.MODE_APPEND);
}
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void read() {
FileInputStream in = null;
BufferedReader reader = null;
StringBuilder content = new StringBuilder();
try {
in = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while((line = reader.readLine())!=null){
content.append(line);
}
readContent.setText(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
效果图
输入文本存储:
输入文本并追加: