可以直接保存文件在你的设备内部存储。通常情况下,文件保存在内存属于你应用程序私有的,其他应用程序不能访问(用户也不能)。当你的应用程序被卸载时,这些文件也被移除。
文件一般存储在/data/data/<package name>/files目录下
For example:
String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();
从内存设备中读取文件:
如果你想在编译时保存一个静态文件在你的应用程序,保存这个文件在你的项目 res/raw/ 目录中。 你可以调用openRawResource()方法打开它,通过 R.raw.<filename> 资源ID。 这个方法返回一个 InputStream 对象 你可以使用它用来读取这个文件(但不能写原始文件)
缓存数据:
如果你想缓存一些数据,而不是持续储存,当你的应用程序将要保存临时文件存储时,你可能使用getCacheDir() 方法打开文件这个文件代表你的内部文件夹。
当设备内存空间不足时,Android也许会删除这个存储文件释放空间。 然而,你不能依赖于系统去清理这些文件, 你应始终保持缓存文件和保持合理的缓存空间,例如1MB。 当用户卸载应用程序时 这些文件也应该移除。
使用的一些方法:
getFilesDir(): 获取文件的绝对路径(data/data/<package name>/files/)
getDir(String name, int mode):获取或创建name对应的文件
deleteFile(String name): 通过文件名删除文件
fileList():获取数据文件夹的全部文件
/**
* 内部存储(文件存储管理)
*/
public class InternalStorage {
/* 内存设备中文件存储路径一般在项目下的files文件中 data/data/<package name>/files/ */
Context context_;
private static final int MODE_DEVICES_FILES = 0X0001;
private static final int MODE_DEVICES_CACHE = 0X0002;
public InternalStorage(Context context) {
this.context_ = context;
}
/**
* 获取需要访问或创建的文件(判断文件存储空间)
* @param name
* @param mode
* @return
*/
private File getFile(String name, int mode){
File file = null;
if (mode == MODE_DEVICES_FILES) {
file = new File(context_.getFilesDir(), name);
} else if(mode == MODE_DEVICES_CACHE) {
file = new File(context_.getCacheDir(), name);
}
return file;
}
public void save(String name,String content){
save(name, content, MODE_DEVICES_FILES);
}
/**
* 保存文件到内部存储设备
* @param name
*/
private void save(String name, String content, int mode){
File file = getFile(name, mode);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fos) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String get(String name){
return get(name, MODE_DEVICES_FILES);
}
/**
* 获取文件内容(先判断文件是否存在)
* @param name
* @return
*/
private String get(String name, int mode){
if (isExist(name, mode)) {
File file = getFile(name, mode);
StringBuffer content = new StringBuffer();
FileInputStream fis = null;
ByteArrayOutputStream baos = null;
try {
fis = new FileInputStream(file);
baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
while (fis.read(bytes) != -1) {
baos.write(bytes);
}
content.append(baos.toString());
return content.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fis) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
System.out.println("------- >>> " + name + " : 文件不存在!");
}
return "";
}
/**
* 在内部存储设备文件中追加内容(如果文件存在,否则创建文件)
* @param name
* @param content
*/
public void append(String name, String content){
FileOutputStream fos = null;
try {
fos = context_.openFileOutput(name, Context.MODE_APPEND);
fos.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != fos) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除内部存储设备指定的文件
* @param name
*/
public boolean delete(String name){
boolean isDelete;
try {
isDelete = context_.deleteFile(name);
return isDelete;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 获取数据文件夹下全部文件名称
* @return
*/
public String[] queryAllFile(){
return context_.fileList();
}
/**
* 判断文件是否存在
* @param name
* @return
*/
public boolean isExist(String name, int mode){
String[] names = null;
if (mode == MODE_DEVICES_FILES) {
names = queryAllFile();
} else if(mode == MODE_DEVICES_CACHE){
names = context_.getCacheDir().list();
}
if (null != names) {
for (String string : names) {
if (string.equals(name)) {
return true;
}
}
}
return false;
}
/*
* 临时存储 (其他方法相同)
*/
public void saveCacheFile(String name, String content){
save(name, content, MODE_DEVICES_CACHE);
}
public String getCacheFile(String name){
return get(name, MODE_DEVICES_CACHE);
}
}