[Android SQLite]数据存储与访问 - 内部存储


Android系统允许应用程序创建仅能够自身访问的私有文件,文件保存在设备的内部存储器上,在Linux系统下的/data/data/<package name>/files目录中



系统读取保存的文件



下面来演示程序

  1. public class Android_DBActivity extends Activity implements OnClickListener  
  2. {  
  3.     private final String FILE_NAME = "fileDemo.txt";  
  4.     private TextView labelView;  
  5.     private TextView displayView;  
  6.     private CheckBox appendBox ;  
  7.     private EditText entryText;  
  8.       
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState)  
  11.     {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.         setupViews();  
  15.     }  
  16.   
  17.     private void setupViews()  
  18.     {  
  19.         labelView = (TextView) findViewById(R.id.label);  
  20.         displayView = (TextView) findViewById(R.id.display);  
  21.         appendBox = (CheckBox) findViewById(R.id.append);  
  22.         entryText = (EditText) findViewById(R.id.entry);  
  23.           
  24.         findViewById(R.id.write).setOnClickListener(this);  
  25.         findViewById(R.id.read).setOnClickListener(this);  
  26.           
  27.         entryText.selectAll();  
  28.         entryText.findFocus();  
  29.     }  
  30.   
  31.     @Override  
  32.     public void onClick(View v)  
  33.     {  
  34.           
  35.         switch (v.getId())  
  36.         {  
  37.             case R.id.write:  
  38.                 FileOutputStream fos = null;  
  39.                 try  
  40.                 {  
  41.                     if (appendBox.isChecked())  
  42.                     {  
  43.                         fos = openFileOutput(FILE_NAME, Context.MODE_APPEND);  
  44.                     }else  
  45.                     {  
  46.                         fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);  
  47.                     }  
  48.                       
  49.                     String text = entryText.getText().toString();  
  50.                     fos.write(text.getBytes());  
  51.                     labelView.setText(String.format("文件写入成功,写入长度:%d", text.length()));  
  52.                 } catch (FileNotFoundException e)  
  53.                 {  
  54.                     e.printStackTrace();  
  55.                 } catch (IOException e)  
  56.                 {  
  57.                     e.printStackTrace();  
  58.                 }finally  
  59.                 {  
  60.                     if (fos != null)  
  61.                     {  
  62.                         try  
  63.                         {  
  64.                             fos.flush();  
  65.                             fos.close();  
  66.                         } catch (IOException e)  
  67.                         {  
  68.                             e.printStackTrace();  
  69.                         }  
  70.                     }  
  71.                 }  
  72.                 break;  
  73.                   
  74.             case R.id.read:  
  75.                 displayView.setText("");  
  76.                 FileInputStream fis = null;  
  77.                 try  
  78.                 {  
  79.                     fis = openFileInput(FILE_NAME);  
  80.                     if (fis.available() == 0)  
  81.                     {  
  82.                         return;  
  83.                     }  
  84.                     byte[] buffer = new byte[fis.available()];  
  85.                     while(fis.read(buffer) != -1)  
  86.                     {  
  87.                     }  
  88.                     String text = new String(buffer);  
  89.                     displayView.setText(text);  
  90.                     labelView.setText(String.format("文件读取成功,文件长度:%d", text.length()));  
  91.                 } catch (FileNotFoundException e)  
  92.                 {  
  93.                     e.printStackTrace();  
  94.                 } catch (IOException e)  
  95.                 {  
  96.                     e.printStackTrace();  
  97.                 }  
  98.                   
  99.                 break;  
  100.         }  
  101.     }  
  102. }  

Android系统支持四种文件操作模式

MODE_PRIVATE 私有模式,缺陷模式,文件仅能够被文件创建程序访问,或具有相同UID的程序访问。
MODE_APPEND 追加模式,如果文件已经存在,则在文件的结尾处添加新数据。
MODE_WORLD_READABLE 全局读模式,允许任何程序读取私有文件。
MODE_WORLD_WRITEABLE 全局写模式,允许任何程序写入私有文件。

相关内容