保存inputText到默认文件“data/data/com.example.项目名/files/data”中(路径是默认的)
private void save(String inputText){
FileOutputStream out = null;
BufferedWriter writer = null;
try{
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
}catch (IOException e){
e.printStackTrace();
}finally {
try{
if (writer !=null){
writer.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
从“data/data/com.example.项目名/files/data”中读取String
private 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();
}