資料存放區的三種方式

來源:互聯網
上載者:User

標籤:blog   java   os   io   檔案   ar   資料   art   div   

public class SpActivity extends Activity implements OnClickListener {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSp).setOnClickListener(this);
findViewById(R.id.btnSpRead).setOnClickListener(this);
findViewById(R.id.btnFileWrite).setOnClickListener(this);
findViewById(R.id.btnFileReader).setOnClickListener(this);
findViewById(R.id.btnFileWriteToSd).setOnClickListener(this);
findViewById(R.id.btnRawRead).setOnClickListener(this);
findViewById(R.id.btnSQlite).setOnClickListener(this);
}

@Override

public class SpActivity extends Activity implements OnClickListener {public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btnSp).setOnClickListener(this);findViewById(R.id.btnSpRead).setOnClickListener(this);findViewById(R.id.btnFileWrite).setOnClickListener(this);findViewById(R.id.btnFileReader).setOnClickListener(this);findViewById(R.id.btnFileWriteToSd).setOnClickListener(this);findViewById(R.id.btnRawRead).setOnClickListener(this);findViewById(R.id.btnSQlite).setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btnSp:SharedPreferences sp = this.getSharedPreferences("setting", Context.MODE_PRIVATE);//SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE);SharedPreferences.Editor editor = sp.edit();editor.putString("name", "張三");editor.putInt("age", 24);editor.putFloat("weight", 110.8f);//提交editor.commit();//editor.apply();break;case R.id.btnSpRead:SharedPreferences spReader = getSharedPreferences("setting", Context.MODE_PRIVATE);String name= spReader.getString("name", "N");int age =spReader.getInt("age", 18);float weight=spReader.getFloat("weight", 80f);Toast.makeText(this, name+"--"+age+"--"+weight, Toast.LENGTH_LONG).show();break;case R.id.btnFileWrite:writeFiles();break;case R.id.btnFileReader:readFile();break;case R.id.btnFileWriteToSd:writeFilesToSDCard();break;case R.id.btnRawRead:readRawFile();break;case R.id.btnSQlite:Intent in = new Intent(this, DBOptActivity.class);startActivity(in);break;default:break;}}public void readRawFile(){Resources res = getResources();InputStream is= res.openRawResource(R.raw.a);byte[] buffer = new byte[1024];int len=0;StringBuilder sb = new StringBuilder();try {while((len= is.read(buffer))!=-1){String tmp = new String(buffer, 0, len);sb.append(tmp);}} catch (IOException e) {e.printStackTrace();}finally{try {if(is!=null){is.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();}/** * 寫檔案 */private void writeFiles(){FileOutputStream os =null;try {os =this.openFileOutput("jerei.txt",Context.MODE_APPEND);os.write("姓名:張三".getBytes());os.write("年齡:25".getBytes());os.write("年齡:25".getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(os !=null){try {os.close();} catch (IOException e) {e.printStackTrace();}os=null;}}}public void readFile(){FileInputStream is=null;StringBuilder sb = new StringBuilder();try {is = this.openFileInput("jerei.txt");byte[] buffer = new byte[1024];int len=0;while((len= is.read(buffer))!=-1){String tmp = new String(buffer,0,len);sb.append(tmp);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(is!=null){is.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();}public void writeFilesToSDCard(){//String filePath = "/mnt/sdcard/jerei";String filePath=null;if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){//擷取SDCard根路徑filePath=Environment.getExternalStorageDirectory().toString();filePath=filePath+ File.separator+"jerei"+File.separator+"edu";File fileParent = new File(filePath);if(!fileParent.exists()){fileParent.mkdirs();}FileOutputStream os = null;try {os = new FileOutputStream(new File(fileParent, "a.txt"));os.write("向SDCard中寫入檔案!!".getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(os!=null){os.close();}} catch (IOException e) {e.printStackTrace();}}}}}

  

 

資料存放區的三種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.