android往文件中保存和读取数据
2012-10-30 14:04
489 查看
android往文件中保存和读取数据
博客分类:android—File
有了数据存储 API,您可以使用内部存储器存储数据。信息可以是私有的,您可以有选择地让其他应用程序对之具有读或写的访问权限。本节介绍这个存储私有数据的 API,它使用
android.content.Context.openFileInput、
openFileOutput和
getCacheDir()来高速缓存数据,而不是永久地存储。
清单 20 中的代码片段展示了如何从内部私有存储器读取数据。使得存储器为私有的方法是对
openFileOutput()使用
MODE_PRIVATE。
/** * Writes content to internal storage making the content private to * the application. The method can be easily changed to take the MODE * as argument and let the caller dictate the visibility: * MODE_PRIVATE, MODE_WORLD_WRITEABLE, MODE_WORLD_READABLE, etc. * * @param filename - the name of the file to create * @param content - the content to write */ public void writeInternalStoragePrivate( String filename, byte[] content) { try { //MODE_PRIVATE creates/replaces a file and makes // it private to your application. Other modes: // MODE_WORLD_WRITEABLE // MODE_WORLD_READABLE // MODE_APPEND FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE); fos.write(content); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } |
openFileInput()的使用。
清单
21. 从内部私有存储器读取数据
/** * Reads a file from internal storage * @param filename the file to read from * @return the file content */ public byte[] readInternalStoragePrivate(String filename) { int len = 1024; byte[] buffer = new byte[len]; try { FileInputStream fis = openFileInput(filename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int nrb = fis.read(buffer, 0, len); // read up to len bytes while (nrb != -1) { baos.write(buffer, 0, nrb); nrb = fis.read(buffer, 0, len); } buffer = baos.toByteArray(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } |
清单
22. 从本地私有存储器删除数据
/** * Delete internal private file * @param filename - the filename to delete */ public void deleteInternalStoragePrivate(String filename) { File file = getFileStreamPath(filename); if (file != null) { file.delete(); } } |
回页首
为公共数据使用设备的外部存储器
有了数据存储 API,您可以使用外部存储器存储数据。信息可以是私有的,您可以有选择地让其他应用程序对之具有读或写的访问权限。本节您将对此 API 进行编程,以便使用包括
getExternalStorageState()、
getExternalFilesDir()、
getExternalStorageDirectory()和
getExternalStoragePublicDirectory()在内的很多
API 来存储公共数据。您为公共数据使用下面的路径:
/Android/data/<package_name>/files/。
在使用外部存储器之前,必须看看它是否可用,是否可写。下面两个代码片段展示了测试这些条件的帮助器方法。清单 23 测试外部存储器是否可用。
清单
23. 测试外部存储器是否可用
/** * Helper Method to Test if external Storage is Available */ public boolean isExternalStorageAvailable() { boolean state = false; String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { state = true; } return state; } |
清单
24. 测试外部存储器是否只可读
/** * Helper Method to Test if external Storage is read only */ public boolean isExternalStorageReadOnly() { boolean state = false; String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { state = true; } return state; } |
清单
25. 写到外部内存
/** * Write to external public directory * @param filename - the filename to write to * @param content - the content to write */ public void writeToExternalStoragePublic(String filename, byte[] content) { // API Level 7 or lower, use getExternalStorageDirectory() // to open a File that represents the root of the external // storage, but writing to root is not recommended, and instead // application should write to application-specific directory, as shown below. String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"; if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) { try { File file = new File(path, filename); file.mkdirs(); FileOutputStream fos = new FileOutputStream(file); fos.write(content); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
清单
26. 从外部内存读取数据
/** * Reads a file from internal storage * @param filename - the filename to read from * @return the file contents */ public byte[] readExternallStoragePublic(String filename) { int len = 1024; byte[] buffer = new byte[len]; String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"; if (!isExternalStorageReadOnly()) { try { File file = new File(path, filename); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int nrb = fis.read(buffer, 0, len); //read up to len bytes while (nrb != -1) { baos.write(buffer, 0, nrb); nrb = fis.read(buffer, 0, len); } buffer = baos.toByteArray(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return buffer; } |
清单
27. 从外部内存删除文件
/** * Delete external public file * @param filename - the filename to write to */ void deleteExternalStoragePublicFile(String filename) { String packageName = this.getPackageName(); String path = "/Android/data/" + packageName + "/files/"+filename; File file = new File(path, filename); if (file != null) { file.delete(); } } |
WRITE_EXTERNAL_STORAGE,它通过 AndroidManifest.xml 请求得到(参见 清单
28)。
清单
28.
WRITE_EXTERNAL_STORAGE
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> |
回页首
相关的方法
如果您具有不需要长期永久保存的临时文件,那么可以将这些文件存储在高速缓存中。高速缓存是一种特殊的内存,可以用于存储中小型数据(少于兆字节),但是您一定要知道,取决于有多少内存可用,高速缓存的内容任何时候都可能被清除。
清单 29 展示了一个帮助器方法,它返回到内部内存中高速缓存的路径。
清单
29. 检索到内部内存高速缓存的路径
/** * Helper method to retrieve the absolute path to the application * specific internal cache directory on the file system. These files * will be ones that get deleted when the application is uninstalled or when * the device runs low on storage. There is no guarantee when these * files will be deleted. * * Note: This uses a Level 8+ API. * * @return the absolute path to the application specific cache * directory */ public String getInternalCacheDirectory() { String cacheDirPath = null; File cacheDir = getCacheDir(); if (cacheDir != null) { cacheDirPath = cacheDir.getPath(); } return cacheDirPath; } |
清单
30. 检索到外部内存高速缓存的路径
/** * Helper method to retrieve the absolute path to the application * specific external cache directory on the file system. These files * will be ones that get deleted when the application is uninstalled or when * the device runs low on storage. There is no guarantee when these * files will be deleted. * * Note: This uses a Level 8+ API. * * @return the absolute path to the application specific cache * directory */ public String getExternalCacheDirectory() { String extCacheDirPath = null; File cacheDir = getExternalCacheDir(); if (cacheDir != null) { extCacheDirPath = cacheDir.getPath(); } return extCacheDirPath; } |
android往文件中保存和读取数据
android 获取FileOutputStream
Context.openFileOutput(文件,权限)
Context为环境的上下文,Activity 继承了Ccontext类 比如 aa 继承了Activity 那就可以这么写 :aa.this.openFileOutput(文件,权限)
文件:既为自己创建文件的名字和文件扩展名,比如 water.txt
权限:android有四种权限
1.Context.MODE_PRIVATE
私有属性,只有自己可以访问,并且第二次写入的内容会覆盖第一次写入的内容
2.Context.MODE_APPEND
私有属性,只有自己可以访问,第二次写入的内容会追加到第一次写入的内容的后面
3.Context.MODE_WORLD_WRITEABLE
公有属性,其它项目都可以写入,不过第二次写入的内容会覆盖第一次写入的内容
4.Context.MODE_WORLD_READABLE
公有属性,其它项目都可以读取
权限是可以相加的,比如
现在我想要一个其它项目可以读取,并且也可以写入,还可以追加
Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE+Context.MODE_APPEND
android 创建的文件会放到手机上的/data/data/包名/files下
android 获取FileInputStream
Context.openInFileOutput(文件)
Context为环境的上下文,Activity 继承了Context类
相关文章推荐
- android 将数据保存到文件中&从文件中读取数据
- android往文件中保存和读取数据
- android 打开pdf文件 从inputStream读取数据并保存为文件
- Android存储数据之一:文件保存与读取
- android,保存文件到手机内存和读取
- android 从文件制定位置读取数据
- Android关于Blockly对Workspace中的block数据保存及读取的流程,及改造原生代码实现Trash垃圾桶中的block保存及读取
- Android数据保存的3种方式 SharedPreferences存储、文件存储、数据库存储SQLite 未完
- Android 之 使用File类在SD卡中读取数据文件
- Android - 读取文件存储的数据
- Android使用文件保存数据,采用比特流保存(解决“\n”无效)
- asp.net怎么读取或保存xml文件里的数据范例
- Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数
- android保存文件到sd卡,读取和清空记录功能(可以用来保存用户名和密码)
- C#笔记(4)保存数据到文件,从文件读取数据
- Android读取assets目录下文件数据内容
- quick-x之android 下保存与读取配置文件
- Android文件的保存(本地和SD卡)与读取
- 从零学python系列之从文件读取和保存数据(转)
- Android入门学习笔记(一)|基础知识|文件数据存储读取|解析XML