Android檔案讀寫,儲存資料
來源:互聯網
上載者:User
package com.king.android.db;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import com.king.android.R;/** * 描述:檔案儲存體 * 作者:Andy.Liu * 時間: 2012-7-9 上午08:06:23 **/public class FileStoreActivity extends Activity implements OnClickListener{/**儲存字串的key*/public static final String STRING_KEY ="string_key";/**儲存字串的的名字*/public static final String STRING_NAME ="string_name";public static final String FILE_NAME = "hello.txt";//儲存的檔案名稱Context mContext = null;EditText edSave,edRead;Button btnSave,btnRead;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mContext = FileStoreActivity.this;initView();}private void initView(){setContentView(R.layout.store_layout);edSave = (EditText) findViewById(R.id.ed_edit);edRead = (EditText) findViewById(R.id.ed_show);btnSave = (Button) findViewById(R.id.btn_save);btnSave.setOnClickListener(this);btnRead = (Button) findViewById(R.id.btn_read);btnRead.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch(v.getId()){case R.id.btn_save:FileStoreTools.saveFile(edSave.getText().toString(), FileStoreTools.getSDPath()+File.separator+FILE_NAME);break;case R.id.btn_read:edRead.setText(FileStoreTools.readFile(FileStoreTools.getSDPath()+File.separator+FILE_NAME));break;}}final static class FileStoreTools{/** * *TODO:儲存檔案 根目錄 *Author:Andy.Liu *Create Time:2012-7-10 上午08:54:14 *TAG:@return *Return:String */public static String getSDPath(){boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);if(hasSDCard){return Environment.getExternalStorageDirectory().toString();}elsereturn Environment.getDownloadCacheDirectory().toString();}/** * *TODO:儲存檔案 *Author:Andy.Liu *Create Time:2012-7-10 上午08:42:40 *TAG:@param str檔案的內容 *TAG:@param filePath儲存路徑 *Return:void */public static void saveFile(String str,String filePath){FileOutputStream fos = null;try {File file = new File(filePath);if(!file.exists())file.mkdirs();fos = new FileOutputStream(file);fos.write(str.getBytes());fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();}catch(IOException e){e.printStackTrace();}finally{try {if(null!=fos)fos.close();} catch (IOException e) {e.printStackTrace();}}}/** * *TODO:讀取檔案 *Author:Andy.Liu *Create Time:2012-7-10 上午08:48:40 *TAG:@param filePath *TAG:@return *Return:String */public static String readFile(String filePath){FileInputStream fis = null;byte[] mByte = new byte[512];try {fis = new FileInputStream(new File(filePath));fis.read(mByte);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(null!=fis)fis.close();} catch (IOException e) {e.printStackTrace();}}return new String(mByte).toString();}}}