zoukankan      html  css  js  c++  java
  • android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput

    一:第一种方式就是像Java平台下的实现方式一样通过构造器直接创建,如果需要向打开的文件末尾写入数据,可以通过使用构造器FileOutputStream(File file, boolean append)将 append设置为true来实现。不过需要注意的是采用这种方式获得FileOutputStream 对象时如果文件不存在或不可写入时,会抛出 FileNotFoundException 异常。

      二:第二种获取 FileInputStream 和 FileOutputStream 对象的方式是调用 Context.openFileInput 和 Context.openFileOutput两个方法来创建。除了这两个方法外,Context对象还提供了其他几个用于对文件操作的方法,如下所示


    Context对象中文操作的API及说明

    方法名 说明
    openFileInput(String filename) 打开应用程序私有目录下的的指定私有文件以读入数据,返回一个FileInputStream 对象
    openFileOutput

    打开应用程序私有目录下的的指定私有文件以写入数据,返回一个FileOutputStream 对象,

    如果文件不存在就创建这个文件。

    fileList() 搜索应用程序私有文件夹下的私有文件,返回所有文件名的String数组
    deleteFile(String fileName) 删除指定文件名的文件,成功返回true,失败返回false


    在使用openFileOutput方法打开文件以写入数据时,需要指定打开模式。默认为零,即MODE_PRIVATE。不同的模式对应的的含义如下:

    openFileOutput方法打开文件时的模式

    常量   含义
    MODE_PRIVATE 默认模式,文件只可以被调用该方法的应用程序访问
    MODE_APPEND   如果文件已存在就向该文件的末尾继续写入数据,而不是覆盖原来的数据。
    MODE_WORLD_READABLE 赋予所有的应用程序对该文件读的权限。
    MODE_WORLD_WRITEABLE   赋予所有的应用程序对该文件写的权限。


       下面通过一个小例子来说明Android平台下的文件I/O 操作方式,主要功能是在应用程序私有的数据文件夹下创建一个文件并读取其中的数据显示到屏幕的 TextView中,这个例子也比较简单只有一个类。
    先看一下运行后的效果吧。

    1. package jcodecraeer.com;
    2. import java.io.FileInputStream;
    3. import java.io.FileOutputStream;
    4. import org.apache.http.util.EncodingUtils;
    5. import android.app.Activity;
    6. import android.graphics.Color;
    7. import android.os.Bundle;
    8. import android.widget.TextView;
    9. public class Activity01 extends Activity{
    10. //常量,为编码格式
    11. public static final String ENCODING = "UTF-8";
    12. //定义文件的名称
    13. String fileName = "test.txt";
    14. //写入和读出的数据信息
    15. String message = "欢迎大家来www.jcodecraeer.com";
    16. TextView textView;
    17. @Override
    18. protected void onCreate(Bundle savedInstanceState) {
    19. super.onCreate(savedInstanceState);
    20. setContentView(R.layout.main);
    21. writeFileData(fileName, message);
    22. String result = readFileData(fileName);
    23. textView = (TextView)findViewById(R.id.tv);
    24. textView.setTextColor(Color.GREEN);
    25. textView.setTextSize(20.0f);
    26. textView.setText(result);
    27. }
    28. //向指定的文件中写入指定的数据
    29. public void writeFileData(String filename, String message){
    30. try {
    31. FileOutputStream fout = openFileOutput(filename, MODE_PRIVATE);//获得FileOutputStream
    32. //将要写入的字符串转换为byte数组
    33. byte[] bytes = message.getBytes();
    34. fout.write(bytes);//将byte数组写入文件
    35. fout.close();//关闭文件输出流
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. }
    39. }
    40. //打开指定文件,读取其数据,返回字符串对象
    41. public String readFileData(String fileName){
    42. String result="";
    43. try {
    44. FileInputStream fin = openFileInput(fileName);
    45. //获取文件长度
    46. int lenght = fin.available();
    47. byte[] buffer = new byte[lenght];
    48. fin.read(buffer);
    49. //将byte数组转换成指定格式的字符串
    50. result = EncodingUtils.getString(buffer, ENCODING);
    51. } catch (Exception e) {
    52. e.printStackTrace();
    53. }
    54. return result;
    55. }
    56. }

     http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0714/1437.html

  • 相关阅读:
    JavaScript模态对话框类
    事件模块的演变(1)
    html5中可通过document.head获取head元素
    How to search for just a specific file type in Visual Studio code?
    What do 'lazy' and 'greedy' mean in the context of regular expressions?
    正则非获取匹配 Lookahead and Lookbehind ZeroLength Assertions
    regex length 正则长度问题
    Inversion of Control vs Dependency Injection
    How to return View with QueryString in ASP.NET MVC 2?
    今天才发现Google Reader
  • 原文地址:https://www.cnblogs.com/chen110xi/p/6629558.html
Copyright © 2011-2022 走看看