android文件读写

来源:互联网 发布:rs232端口是什么意思 编辑:程序博客网 时间:2024/05/17 17:55

android文件的读写主要分为两个方面,一个是将内容写入本应用的data文件夹中,另一个是将内容写入到sdcard中。两者都使用I/O流的读写技术。

下面具体具体介绍这两方面的内容:

一。将内容写入本应用中:

/** * @description:将内容保存到内置存储中 * @author:Administrator * @return:boolean * @param fileName *            文件名 * @param fileContent *            文件内容 * @param context *            上下文对象 * @return */public static boolean fileSaveApp(String fileName, String fileContent,Context context) {try {// 用android提供的输出流来将内容写入到文件中,注意mode的用途FileOutputStream fos = context.openFileOutput(fileName,context.MODE_APPEND);fos.write(fileContent.getBytes());fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return false;}


注意android的上下文对象context在这个方法中起的的作用(可以用它来打开一个输出流,或者打开一个输入流),还有文件的写入模式。这里用的是文件的追加模式。数据时只可被本应用操作的。

 

二。将数据写入sdcard中

 

/** * @description:将内容写入到sdcard的文件当中 * @author:Administrator * @return:boolean * @param fileName *            文件名 * @param fileContent *            文件内容 * @param path *            文件路径 * @return */public static boolean fileSave(String fileName, String fileContent,File path) {File file = new File(path, fileName);if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}FileOutputStream fos = null;int count = 0;try {fos = new FileOutputStream(file);count = fileContent.getBytes().length;fos.write(fileContent.getBytes(), 0, count);fos.close();return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return false;}


注意这个方法中的path参数指的是sdcard的路径:=Environment.getExternalStorageDirectory();

注意写入数据的时候需要判断sdcard是否存在是否可写:Environment.getExternalStorageState().equals(
       Environment.MEDIA_MOUNTED);

最后需要注意如果要向sdcard写入数据必须在manifest.xml加入权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />第一个权限是指对sdcard可写,第二个权限是指可以在sdcard中建立和删除文件。

 

最后将从应用的data中读取数据的方法也写进来:

 

/** * @description:读取本应用data文件夹中文件的内容(如果要读取sdcard中的文件时一定要去判断sdcard是否存在,并且是可读的) * @author:Administrator * @return:String 文件的内容 * @param fileName *            文件名 * @param context *            上下文对象 * @return */public static String readFile(String fileName, Context context) {String str = "";try {FileInputStream fis = context.openFileInput(fileName);ByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = fis.read(buffer)) != -1) {// 用字节输出流将读到的内容写入到内存中bos.write(buffer, 0, len);}// 将输出流中的数据转换为Stringstr = new String(bos.toByteArray(), "utf-8");} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return str;}


这里就不再介绍从sdcard中读取文件内容的方法了。

 

 

这里将android本应用的文件存储的4中方式列举出来:可以参考 http://www.cnblogs.com/jqyp/archive/2010/09/13/1825249.html

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入:
openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);

 

原创粉丝点击