Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android文件存儲詳細完整示例

Android文件存儲詳細完整示例

編輯:關於Android編程

MainActivity如下:

package cc.cv;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * Demo描述:
 * 文件存儲詳細示例
 * 
 * 可利用Context的openFileOutput()方法將數據存儲到文件中.
 * 默認的存儲路徑為/data/data//files/
 * 可利用Context的openFileInput()方法將數據從文件中讀出
 * 
 * 在Android中常用的存儲方式:
 * SharedPreferences,數據庫,SD卡,文件存儲.
 * 對於最後一種方式平時用的不多,現在此寫個例子
 * 整理和復習一下.
 * 
 * 注意事項:
 * 文件存儲是不需要權限的與在SD卡上進行文件存儲是不一樣的
 */
public class MainActivity extends Activity {
    private Button mSaveButton;
    private Button mGetButton;
    private Context mContext;
    private String mFileName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		mContext=this;
		mFileName="testData";
		mSaveButton=(Button) findViewById(R.id.saveButton);
		mSaveButton.setOnClickListener(new ClickListenerImpl());
		mGetButton=(Button) findViewById(R.id.getButton);
		mGetButton.setOnClickListener(new ClickListenerImpl());
	}

	private class ClickListenerImpl implements OnClickListener{
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.saveButton:
				saveDataToFile(mContext,mFileName);
				break;
			case R.id.getButton:
				getDataFromFile(mContext,mFileName);
				break;
			default:
				break;
			}
		}
		
	}
	
	/**
	 * 向File中保存數據
	 */
	private void saveDataToFile(Context context,String fileName){
		String data="Hello,All";
		FileOutputStream fileOutputStream=null;
		OutputStreamWriter outputStreamWriter=null;
		BufferedWriter bufferedWriter=null;
		try {
			fileOutputStream=context.openFileOutput(fileName, Context.MODE_PRIVATE);
			outputStreamWriter=new OutputStreamWriter(fileOutputStream);
			bufferedWriter=new BufferedWriter(outputStreamWriter);
			bufferedWriter.write(data);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if (bufferedWriter!=null) {
					bufferedWriter.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 * 從File中讀取數據
	 */
	private void getDataFromFile(Context context,String fileName){
		FileInputStream fileInputStream=null;
		InputStreamReader inputStreamReader=null;
		BufferedReader bufferedReader=null;
		StringBuilder stringBuilder=null;
		String line=null;
		try {
			stringBuilder=new StringBuilder();
			fileInputStream=context.openFileInput(fileName);
			inputStreamReader=new InputStreamReader(fileInputStream);
			bufferedReader=new BufferedReader(inputStreamReader);
			while((line=bufferedReader.readLine())!=null){
				stringBuilder.append(line);
			}
			System.out.println("--->"+stringBuilder.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if (bufferedReader!=null) {
					bufferedReader.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	

}

main.xml如下:


    


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved