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

android 学习笔记 数据存储到文件中

2015-08-25 09:00 507 查看
<pre name="code" class="java">           public void save() {

String data = "Data to save";
FileOutputStream out = null;

BufferedWriter writer = null;
try {

out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));

writer.write(data);
} catch (IOException e) {

e.printStackTrace();
} finally {

try {
if (writer != null) {

writer.close();
}

} catch (IOException e) {
e.printStackTrace();

}
}

}


上面代码是保存文件 

获取文件的内容

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();

}

}
这个是用java 流来进行读写操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android