Android读取文件
16lz
2021-12-04
转载请注意申明地址,
http://blog.csdn.net/lib739449500/article/details/42836181
珍惜主人的劳动成果。
在实际开发中,Android提供了5种方式存储数据 1.文件存储数据 2.使用SharedPreferences存储数据 3. SQLite数据库存储数据 4. 使用ContentProvider存储数据 5. 网络存储数据
接下来就自己总结先写下文件存储数据这种方式。 就文件存储这种方式也分好几种情况。 1。读取应用程序的数据文件夹里的文件
注意:资源文件只能读不能写 4).读取asset文件夹中获取文件 InputStream in = getResources (). getAssets ().open( fileName ); EncodingUtils.getString (buffer, “UTF-8”);// 解决中文乱码的问题 Android 获取 assets 的绝对路径 第一种方法: String path = "file:///android_asset/ 文件名 "; 第二种方法: InputStream path = getClass (). getResourceAsStream ("/assets/ 文件名 "); 注意:资源文件只能读不能写
转载请注意申明地址,
http://blog.csdn.net/lib739449500/article/details/42836181
珍惜主人的劳动成果。
http://blog.csdn.net/lib739449500/article/details/42836181
珍惜主人的劳动成果。
在实际开发中,Android提供了5种方式存储数据 1.文件存储数据 2.使用SharedPreferences存储数据 3. SQLite数据库存储数据 4. 使用ContentProvider存储数据 5. 网络存储数据
接下来就自己总结先写下文件存储数据这种方式。 就文件存储这种方式也分好几种情况。 1。读取应用程序的数据文件夹里的文件
可通过Context的openFileOutput或openFileInput来打开文件的输入输出流
FileInputStream openFileInput (String name) :打开应用程序的数据文件夹下的 name 文件对应的输入流 FileOutStreamopenFileOutput(Stringname,int mode):打开应用程序的数据的文件夹下的name文件对应输出流 第一个参数为文件名,不能包含路径分割符“/”
其中第二个方法的第二个参数是对文件的访问权限, Context.MODE_PRIVATE = 0\\ 是应用程序私有的 Context.MODE_APPEND =32768 \\ 追加 如果要创建的文件存在则新写入的数据不会覆盖以前的数据 Context.MODE_WORLD_READABLE = 1 \\ 可读 所有应用程序都可以访问的 Context.MODE_WORLD_WRITEABLE = 2 \\ 可写 所有应用程序都可以写
如果希望有多个权限,则写成如下形式: Context.MODE_PRIVATE + Context.MODE_APPEND (注意是用“+”号) 如果文件不存在,Android会自动创建它。
创建的文件保存在/data/data/包名/files目录,
除此之外, Context 还提供了如下几个方法来访问应用程序的数据文件夹: getDir (String name,int mode) 在应用程序的数据文件夹下获取或者创建 name 对应的子目录 File getFilesDir () 获取该应用程序的数据文件夹的绝对路径 String[]fileList()返回该应用程序的数据文件夹下的指定文件 deleteFile (String) 删除该应用程序的数据文件夹下的指定文件
补充:如果您要读取其他应用的文件的话,你要完整写出这个文件的路径,如:
如/data/data/com.henii.android/files/myText.txt,
而且不能使用openFileInput方法(因为这个方法的参数是不能带分隔符“/”的)
所以您只能使用最一般的读取文件的方法进行读取 简单贴一下自己的测试代码:
/** * 写入方法 * * @param fileName * 文件的名称 * @param content * 文件的内容 * @return */public boolean save(String fileName, String content) {boolean flag = false;try {FileOutputStream outStream = context.openFileOutput(fileName,Context.MODE_PRIVATE + Context.MODE_APPEND);outStream.write(content.getBytes());flag = true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return flag;}/** * 读取方法 * * @param fileName * 文件的名称 * @return */public String read(String fileName) {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();try {FileInputStream inStream = context.openFileInput(fileName);int len = 0;byte[] data = new byte[1024];while ((len = inStream.read(data)) != -1) {outputStream.write(data, 0, len);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return new String(outputStream.toByteArray());}
转载请注意申明地址,
http://blog.csdn.net/lib739449500/article/details/42836181
珍惜主人的劳动成果。
2).读取sd卡上的文件
很简单。只需两步即可。
一是添加权限。
<!-- 允许用户创建和删除sdcard卡上的内容-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<!-- 允许用户往sdcard卡上写入的内容-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
二是开始写代码即可。但读写之前得写通过Environment的getExternalStorageDirectory方法来判断手机上是否插了SD卡,并且应用程序具有读写SD卡的权限。
简单贴一下读写代码:
public String testReadSdcard(String fileName) {FileInputStream inputStream = null;// 缓存的流,和磁盘无关,不需要关闭ByteArrayOutputStream outputStream = new ByteArrayOutputStream();File file = new File(Environment.getExternalStorageDirectory(),fileName);if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {try {inputStream = new FileInputStream(file);int len = 0;byte[] data = new byte[1024];while ((len = inputStream.read(data)) != -1) {outputStream.write(data, 0, len);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}}return new String(outputStream.toByteArray());}/** * @param fileName * 文件的名称 * @param content * 文件的内容 * @return */public boolean tesWriteSdcard(String fileName, String content) {boolean flag = false;FileOutputStream fileOutputStream = null;// 获得sdcard卡所在的路径File file = new File(Environment.getExternalStorageDirectory(),fileName);// 判断sdcard卡是否可用if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {try {fileOutputStream = new FileOutputStream(file, true);fileOutputStream.write(content.getBytes());flag = true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}return flag;}
3).读取raw文件夹中获取文件
InputStream in = getResources (). openRawResource ( R.raw.a ); EncodingUtils.getString(buffer, “UTF-8”);//解决中文乱码的问题注意:资源文件只能读不能写 4).读取asset文件夹中获取文件 InputStream in = getResources (). getAssets ().open( fileName ); EncodingUtils.getString (buffer, “UTF-8”);// 解决中文乱码的问题 Android 获取 assets 的绝对路径 第一种方法: String path = "file:///android_asset/ 文件名 "; 第二种方法: InputStream path = getClass (). getResourceAsStream ("/assets/ 文件名 "); 注意:资源文件只能读不能写
转载请注意申明地址,
http://blog.csdn.net/lib739449500/article/details/42836181
珍惜主人的劳动成果。
更多相关文章
- Android(安卓)學習之旅!(2)
- Android应用程序目录结构分析
- Android(安卓)体系结构介绍
- Android(安卓)权限管理 — 只防君子不防小人
- android 架构体系
- 让Activity变成一个窗口:Activity属性设定
- android基础知识15:获得android系统信息01—info类
- Android关于线程更新UI的方法
- Android应用程序开发的五大概念