Android学习 数据存储之_文件存储

正在学习android的数据存储,记录分享如下:


效果图如下



内部存储

To create and write a private file to the internal storage:


CallopenFileOutput()with the name of the file and the operating mode. This returns a FileOutputStream.

Write to the file withwrite().
Close the stream withclose().


To read a file from internal storage:


CallopenFileInput()and pass it the name of the file to read. This returns a FileInputStream.

Read bytes from the file with read().
Then close the stream with  close().

外部


和内部相比 有几点不同

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;
		}
	}

}


参考 https://developer.android.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值