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();}}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.