将数据存储到内存卡

来源:互联网 发布:c罗官方数据 编辑:程序博客网 时间:2024/05/27 10:43

实验代码:

package com.example.test1102;    import java.io.BufferedReader;  import java.io.BufferedWriter;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.InputStreamReader;  import java.io.OutputStreamWriter;    import android.app.Activity;  import android.content.Context;  import android.os.Bundle;  import android.os.Environment;  import android.text.TextUtils;  import android.view.Menu;  import android.widget.EditText;  import android.widget.Toast;    public class MainActivity extends Activity  {    private EditText edit;      @Override      protected void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          edit = (EditText) findViewById(R.id.edit);          String inputText=load();          if (!TextUtils.isEmpty(inputText)) {              edit.setText(inputText);              edit.setSelection(inputText.length());              Toast.makeText(this, "Restoring succeeded", Toast.LENGTH_SHORT).show();          }                      if(isHavedSDcard()){                  try {                      writeStr2SDCard(inputText);                  } catch (Exception e) {                      e.printStackTrace();                  }              }              else{                  showToast("您的手机没有内存卡哦!");              }                    }       public boolean isHavedSDcard() {              if (Environment.getExternalStorageState().equals(                      Environment.MEDIA_MOUNTED))                  return true;              else                  return false;          }       public String getRootPath() {              return Environment.getExternalStorageDirectory().toString();          }        public void showToast(String text) {              Toast.makeText(this, text, Toast.LENGTH_SHORT).show();          }                     private void writeStr2SDCard(String str) throws Exception{              FileOutputStream fos = new FileOutputStream(getRootPath()+"/test.txt");              fos.write(str.getBytes());          }        @Override      protected void onDestroy()      {          // TODO Auto-generated method stub          super.onDestroy();          String inputText = edit.getText().toString();          save(inputText);      }        private void save(String inputText)      {          // TODO Auto-generated method stub          FileOutputStream out = null;          BufferedWriter writer = null;          try {              out = openFileOutput("data", Context.MODE_PRIVATE);              writer = new BufferedWriter(new OutputStreamWriter(out));              writer.write(inputText);          } catch (IOException e) {              e.printStackTrace();          } finally {              try {                  if (writer != null) {                      writer.close();                  }              } catch (IOException e) {                  e.printStackTrace();              }          }                }      public String load() {          FileInputStream in = null;          BufferedReader reader = null;          StringBuilder content = new StringBuilder();          try {              in = openFileInput("data");              reader = new BufferedReader(new InputStreamReader(in));              String line = "";              while ((line = reader.readLine()) != null) {                  content.append(line);              }          } catch (IOException e) {              e.printStackTrace();          } finally {              if (reader != null) {                  try {                      reader.close();                  } catch (IOException e) {                      e.printStackTrace();                  }              }          }          return content.toString();      }          @Override      public boolean onCreateOptionsMenu(Menu menu)      {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.main, menu);          return true;      }    }  


0 0
原创粉丝点击