[Android SQLite]数据存储与访问

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


[Android SQLite]数据存储与访问


系统读取保存的文件

[Android SQLite]数据存储与访问



下面来演示程序

[Android SQLite]数据存储与访问

public class Android_DBActivity extends Activity implements OnClickListener   {       private final String FILE_NAME = "fileDemo.txt";       private TextView labelView;       private TextView displayView;       private CheckBox appendBox ;       private EditText entryText;              @Override       public void onCreate(Bundle savedInstanceState)       {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);           setupViews();       }          private void setupViews()       {           labelView = (TextView) findViewById(R.id.label);           displayView = (TextView) findViewById(R.id.display);           appendBox = (CheckBox) findViewById(R.id.append);           entryText = (EditText) findViewById(R.id.entry);                      findViewById(R.id.write).setOnClickListener(this);           findViewById(R.id.read).setOnClickListener(this);                      entryText.selectAll();           entryText.findFocus();       }          @Override       public void onClick(View v)       {                      switch (v.getId())           {               case R.id.write:                   FileOutputStream fos = null;                   try                   {                       if (appendBox.isChecked())                       {                           fos = openFileOutput(FILE_NAME, Context.MODE_APPEND);                       }else                       {                           fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);                       }                                              String text = entryText.getText().toString();                       fos.write(text.getBytes());                       labelView.setText(String.format("文件写入成功,写入长度:%d", text.length()));                   } catch (FileNotFoundException e)                   {                       e.printStackTrace();                   } catch (IOException e)                   {                       e.printStackTrace();                   }finally                   {                       if (fos != null)                       {                           try                           {                               fos.flush();                               fos.close();                           } catch (IOException e)                           {                               e.printStackTrace();                           }                       }                   }                   break;                                  case R.id.read:                   displayView.setText("");                   FileInputStream fis = null;                   try                   {                       fis = openFileInput(FILE_NAME);                       if (fis.available() == 0)                       {                           return;                       }                       byte[] buffer = new byte[fis.available()];                       while(fis.read(buffer) != -1)                       {                       }                       String text = new String(buffer);                       displayView.setText(text);                       labelView.setText(String.format("文件读取成功,文件长度:%d", text.length()));                   } catch (FileNotFoundException e)                   {                       e.printStackTrace();                   } catch (IOException e)                   {                       e.printStackTrace();                   }                                      break;           }       }   }  

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyfwwj.html