File文件存储方式的工具类:
/**
* Created by admin on 2016/12/13.
*/
public class FileWRTools {
/**
* 输出流形式,来保存文件
* @param context
* @param data 要保存的字符串
* “data” 是要要生成的文件的文件名(data.txt)
*/
public static void writeFile(Context context, String data) {
BufferedWriter bufferedWriter = null;
FileOutputStream outputStream;
try {
outputStream = context.openFileOutput("data", Context.MODE_PRIVATE);
bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null)
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 输入流形式,来读取data文件
* @param context
* @return
*/
public static String readFile(Context context){
FileInputStream inputStream ;
BufferedReader bufferedReader = null;
StringBuffer stringBuffer = new StringBuffer();
try {
inputStream = context.openFileInput("data");
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line ="";
while ((line = bufferedReader.readLine()) != null){
stringBuffer.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuffer.toString();
}
}