正在学习android的数据存储,记录分享如下:
效果图如下
内部存储
CallopenFileOutput()with the name of the file and the operating mode. This returns a FileOutputStream.
Write to the file withwrite().
CallopenFileInput()and pass it the name of the file to read. This returns a FileInputStream.
Read bytes from the file with read().外部
和内部相比 有几点不同
1.查看外部存储设备是否可用,getExternalStorageState()
2.在清单文件中要加上读写外部存储的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
3.指定具体的存储路径,比如用绝对存储路径
File newfile =new File(dir.getAbsolutePath() + "/" + exFileName);
主要代码如下【仅供参考】
public class MainActivity extends ActionBarActivity { private static final String FILE_NAME = "fileInDome.txt"; private static final String TAG = "com.example.filestorage.MainActivity"; private EditText etInput; private EditText etContent; private static boolean isRbIn = true; private String exFileName = "fileExDome.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etInput = (EditText) findViewById(R.id.et_files); etContent = (EditText) findViewById(R.id.et_content); Button btRead = (Button) findViewById(R.id.bt_read); Button btWrite = (Button) findViewById(R.id.bt_write); btRead.setOnClickListener(new btClickListener()); btWrite.setOnClickListener(new btClickListener()); } private class btClickListener implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_read: read_files(); break; case R.id.bt_write: write_files(); break; default: break; } } } public void read_files() { if (isRbIn) { FileInputStream fis = null; try { fis = openFileInput(FILE_NAME); if (fis.available() == 0) return; byte[] readBytes = new byte[fis.available()]; while (fis.read(readBytes) != -1) ; String readContent = new String(readBytes); etContent.setText(readContent); Toast.makeText(this, "文件读取成功,文件长度为:" + readContent.length(), 0) .show(); fis.close(); } catch (Exception e) { e.printStackTrace(); } } else { // 外部存储 FileInputStream fis =null; Log.i(TAG, "读取外部存储 isExternalStorageWritable"); if (isExternalStorageWritable()) { File dir = new File("/sdcard/"); Log.i(TAG, "读取外部存储 /sdcard/"); if(dir.exists() && dir.canWrite()){ Log.i(TAG, "读取外部存储"); try { fis = new FileInputStream(dir.getAbsolutePath() + "/" + exFileName); if(fis.available() == 0) return; Log.i(TAG, "" + fis.available()); byte[] readBytes =new byte[fis.available()]; while(fis.read(readBytes) !=-1) ; String readContent =new String(readBytes); etContent.setText(readContent); Toast.makeText(this, "从SD卡读取成功,文件长度为:" + readContent.length(), 0).show(); etInput.setText(""); fis.close(); } catch (Exception e) { e.printStackTrace(); } } } else { Toast.makeText(this, "外部存储不可写", 0).show(); } } } public void write_files() { if (isRbIn) { FileOutputStream fos = null; try { // 内部存储 fos = openFileOutput(FILE_NAME, MODE_PRIVATE); String text = etInput.getText().toString(); fos.write(text.getBytes()); Toast.makeText(this, "文件写入成功,文件长度为:" + text.length(), 0).show(); etInput.setText(""); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.flush(); fos.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } else { // 外部存储 FileOutputStream fos =null; Log.i(TAG, "写入外部存储 isExternalStorageWritable"); if (isExternalStorageWritable()) { File dir = new File("/sdcard/"); Log.i(TAG, "写入外部存储 /sdcard/"); if(dir.exists() && dir.canWrite()){ Log.i(TAG, "写入外部存储"); File newfile =new File(dir.getAbsolutePath() + "/" + exFileName); try { fos = new FileOutputStream(newfile); String text = etInput.getText().toString(); fos.write(text.getBytes()); Toast.makeText(this, "文件写入SD卡成功,文件长度为:" + text.length(), 0).show(); etInput.setText(""); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } else { Toast.makeText(this, "外部存储不可写", 0).show(); } } } /* Checks if external storage is available for read and write */ public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; } public void onRadioButtonClicked(View view) { switch (view.getId()) { case R.id.rb_in: Log.i(TAG, "内部存储"); isRbIn = true; break; case R.id.rb_ex: Log.i(TAG, "外部存储"); isRbIn = false; break; default: break; } } }