Android文件操作中的openFileOutPut和openFileInput
来源:互联网 发布:淘宝给差评被砍死 编辑:程序博客网 时间:2024/06/06 06:41
概述:
之前我写过一篇博客,叫《Android中SharedPreferences与Editor的使用》。说的是在Android程序中保存数据(多是一些配置参数的数据)的应用。Android中的5种数据存储方式在之前的那篇文章中也有简单介绍。今天,我就来说说Android文件操作中的openFileOutPut和openFileInput。
我想大家或多或少都有学习过一段时间的Java吧,学习Android,Java是基础。今天要写的这个文件操作与Java中文件操作基本上是差不多的。都是要先去创建一个输入流/输出流,然后,我们就是通过从指定文件中按字节来读取文件中的内容到我们的输入流中,再把读进来的数据追加到一个字符串缓存中(这里用的是StringBuilder),当然你要是觉得不喜欢new来new去,也可以直接使用String类型的字符串。
供读文件关键代码:
/** * 读数据 */private void readerDataFromSD() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 获取SDcard路径 StringBuilder sb = new StringBuilder(); try { File sdCardDir = Environment.getExternalStorageDirectory(); File file = new File(sdCardDir, FILE_NAME); InputStream inputStream = new FileInputStream(file); int len = 0; byte[] buffer = new byte[1024]; while ((len = inputStream.read(buffer)) != -1) { sb.append(new String(buffer, 0, len)); } mShowResultTextView.setText(sb.toString()); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } }}
写文件的时候,我们要先创建一个输出流,再将FileOutputStream包装成PrintStream,最后输出。当然,大家也可以不用包装流,直接使用节点流写数据也可以,就像上面读数据一样。
写文件关键代码:
/** * 写数据 */private void writeData2SD() { System.out.println("往SDCard里写数据"); // 先判断是否有SDCard if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { String content = mContentEditText.getText().toString(); try { // 获取SDcard路径 File sdCardDir = Environment.getExternalStorageDirectory(); File file = new File(sdCardDir, FILE_NAME); // File file = new File(sdcardPath + File.separator + FILE_NAME); // 以指定文件创建RandomAccessFile对象 RandomAccessFile raf = new RandomAccessFile(file, "rw"); // 将文件记录指针移动最后 raf.seek(file.length()); // 输出文件内容 raf.write(content.getBytes()); raf.close(); } catch (Exception e) { e.printStackTrace(); } }}大家可以看到上有一句
raf.seek(file.length());它是将文件记录指针移动到最后的位置。所以这种方式就是以追加的方式来写文件了。如果你不想以这种方式来写文件,那就要把这句话除掉。
权限说明:
另外,别忘了添加读写文件的权限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
参考资料:
《疯狂Android讲义》
0 0
- Android文件操作中的openFileOutPut和openFileInput
- Android openFileOutput 和 openFileInput
- Android openFileOutput 和 openFileInput
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- Android之openFileoutput和openFileinput
- android 文件存储(openFileOutPut 和 openFileInput())
- Android数据存储之文件 openFileOutput & openFileInput
- 使用openFileOutput和openFileInput读取文件
- Android下openfileoutput和openfileinput的使用
- openFileOutput和openFileInput
- openFileOutput()和openFileInput
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- 同步环境下使用变量的注意点
- 如何将自定义的没有结束标签的xml转换为javabean对象
- 新媒体营销操作手法及案例分享-初贵民
- QT windows VS C++
- 查找大于100M的文件
- Android文件操作中的openFileOutPut和openFileInput
- Android下白盒自动化回归测试junit
- 二路归并排序mergeSort代码实现_legend
- 使用纯粹的C++语言编写COM组件
- HashMap存取过程中改变key值导致的内存泄露
- 用VC进行COM编程所必须掌握的理论知识
- 枚举进程句柄
- MySql数据库连接时DriverManager.getConnection(url,user,pass)出错
- 很期待ES6