初涉Android之文件保存
16lz
2021-12-04
Android文件保存
一、Android的底层使用Linux内核,文件对文件所有者、与文件所有者同组的其它人、以及其它组的成员分别有可读、可写和可执行三种权限,具体可以参考《Linux私房菜》,当然,学习Android不需要了解这么多啊,在Android中,文件操作大致有四种操作模式,分别是MODE_PRIVATE、MODE_APPEND、MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE等。
下面对于四种操作模式进行简单的介绍:
- MODE_PRIVATE(私有操作模式):创建出来的文件只有本应用能够访问,其它应用不能访问,另外用私有操作模式生成的文件,写入文件的内容会覆盖以前已有的内容
- MODE_APPEND(追加操作模式):创建出来的文件只有本应用能够访问,其它应用不能访问,另外用追加操作模式生成的文件,写入文件的内容会追加在以前文件的前面
- MODE_WORLD_READABLE(可读操作模式):创建出来的文件所有应用都能够访问,另外用可读操作模式生成的文件,写入文件的内容会覆盖掉原来的内容,但是不能修改内容
- MODE_WORLD_WRITEABLE(可写操作模式):创建出来的文件其它应用不能够访问,另外用可写操作模式生成的文件,写入文件的内容会覆盖掉原来的内容,其它应用也可以修改内容
public class FileService {private Context context = null;private static final String TAG = "FileService";public FileService(Context context) {this.context = context;}/* * 私有操作模式保存文件 */public void savePrivate(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_PRIVATE);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}/* * 追加操作模式保存文件 */public void saveAppend(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_APPEND);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}/* * 可读操作模式保存文件 */public void saveReadable(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_WORLD_READABLE);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}/* * 可写操作模式保存文件 */public void saveWritable(String nameText, String contentText) throws IOException {OutputStream out = context.openFileOutput(nameText, Context.MODE_WORLD_WRITEABLE);Log.i(TAG,nameText);out.write(contentText.getBytes());out.close();}}
二、文件的读取
读取文件的时候,如果读取文件是当前应用底下的文件可以使用Context对象来获得文件输入流,如果是读取其它目录的文件,自己创建一个文件读取流即可,读取相对比较简单,就不多做介绍。
/** * 读取文件内容 * @param fileName 文件名 * @return java.lang.String 返回文件内容 * @throws IOException 抛出的IO流异常 */public String read(String fileName) throws IOException {InputStream is = context.openFileInput(fileName);byte[] buf = new byte[1024];ByteArrayOutputStream baos = new ByteArrayOutputStream();int len = 0;while((len = is.read(buf)) != -1) {baos.write(buf , 0 , len);}is.close();byte[] res = baos.toByteArray();baos.close();return new String(res);}
/** * 读取非当前应用底下的文件 * @return java.lang.String 返回文件内容 * @throws IOException 抛出IO流异常 */public String read() throws IOException {String path = PATH + fileName;File file = new File(path);FileInputStream fis = new FileInputStream(file);ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buf = new byte[1024];int len = 0;while((len = fis.read(buf)) != -1) {baos.write(buf , 0 , len);}byte[] result = baos.toByteArray();return new String(result);}
三、保存文件到存储卡(简略介绍)
/** * 保存文件到存储卡设备 * @param name 要保存的文件名 * @param content 文件内容 * @throws IOException 抛出IO流异常 */public void saveSdcard(String name, String content) throws IOException {File file = new File("/mnt/sdcard/" + name);FileOutputStream fos = new FileOutputStream(file);fos.write(content.getBytes());fos.close();}
更多相关文章
- Android(安卓)layout文件中 '?' 的作用
- 修改 framework 代码的经验和踩过的坑
- 在Android设备与Mac电脑之间传输文件
- 如何安装apk文件在Android仿真器中
- 《Android/OPhone开发完全讲义》连载(4):Android(安卓)SDK中常用命
- Android(安卓)Jni代码示例讲解
- android中的数据库操作
- Android(安卓)Studio安卓学习笔记(二)Android项目结构
- 后台(02)——MySQL(2)