`

android文件存储文本

 
阅读更多
/**
* 写入文本
*/
private void inputTxt(String txt){
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("txt", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(txt);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 读取文本
*/
private String getFileTxt(){
FileInputStream in = null;
BufferedReader reader = null;
StringBuffer content = new StringBuffer();
try {
in = openFileInput("txt");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
content.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics