为什么要写一个SD的读写接口呢?
最近在看一本书叫做《大话设计模式》,里面的各种设计模式,让我感觉到代码要写的好是要经过很多次的修改的,整合出来的思想,或者好的代码,要复用,这样子开发的效率才能提高。
以前在写对SD的读写时,都是需要什么就用什么,这样子每一次都要写相同的代码。
所以今天总结一下对SD读写的接口
public abstract class SDEngine {
protected File dirFile = null; //保存的文件夹
protected File file = null; //保存的文件
public boolean createFile(String str_dir_name, String str_file_name) {
String str_dir_path = Environment.getExternalStorageDirectory() + "/" + str_dir_name + "/" ;
dirFile = new File(str_dir_path);
if(!dirFile.exists()) {
dirFile.mkdirs();
}
file = new File(str_dir_path + str_file_name);
if (file.exists()) {
return true;
} else {
try {
if(file.createNewFile()) {
return true;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();