类似于将数据存储到文件中,Context 类中还提供了一个 openFileInput()方法,用于从文件中读取数据。这个方法要比 openFileOutput()简单一些,它只接收一个参数,即要读取的文件名,然后系统会自动到/data/data/<package name>/files/目录下去加载这个文件,并返回一个FileInputStream 对象,得到了这个对象之后再通过 Java 流的方式就可以将数据读取出来了。以下是一段简单的代码示例,展示了如何从文件中读取文本数据:
public String load() {
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);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
在这段代码中,首先通过 openFileInput()方法获取到了一个 FileInputStream 对象,然后
借助它又构建出了一个 InputStreamReader对象,接着再使用 InputStreamReader 构建出一个
BufferedReader 对象,这样我们就可以通过 BufferedReader 进行一行行地读取,把文件中所
有的文本内容全部读取出来并存放在一个 StringBuilder 对象中,最后将读取到的内容返回就
可以了。
了解了从文件中读取数据的方法,那么我们就来继续完善上一小节中的例子,使得重新
启动程序时 EditText中能够保留我们上次输入的内容。修改 MainActivity中的代码,如下所示:
public class MainActivity extends Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
String inputText = load();
if (!TextUtils.isEmpty(inputText)) {
edit.setText(inputText);
edit.setSelection(inputText.length());
Toast.makeText(this, "Restoring succeeded",
Toast.LENGTH_SHORT).show();
}
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);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
}
public String load() {
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);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
在这段代码中,首先通过 openFileInput()方法获取到了一个 FileInputStream 对象,然后
借助它又构建出了一个 InputStreamReader对象,接着再使用 InputStreamReader 构建出一个
BufferedReader 对象,这样我们就可以通过 BufferedReader 进行一行行地读取,把文件中所
有的文本内容全部读取出来并存放在一个 StringBuilder 对象中,最后将读取到的内容返回就
可以了。
了解了从文件中读取数据的方法,那么我们就来继续完善上一小节中的例子,使得重新
启动程序时 EditText中能够保留我们上次输入的内容。修改 MainActivity中的代码,如下所示:
public class MainActivity extends Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
String inputText = load();
if (!TextUtils.isEmpty(inputText)) {
edit.setText(inputText);
edit.setSelection(inputText.length());
Toast.makeText(this, "Restoring succeeded",
Toast.LENGTH_SHORT).show();
}
}
public String load() {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);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
}