Android File資料存放區

來源:互聯網
上載者:User

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6 <EditText
7 android:id="@+id/write_edit"
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:lines="4"
11 android:gravity="top"
12 />
13 <Button
14 android:layout_width="wrap_content"
15 android:layout_height="wrap_content"
16 android:text="Save"
17 android:onClick="writeFile"
18 />
19 <EditText
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:lines="4"
23 android:id="@+id/read_edit"
24 android:gravity="top"
25 android:editable="false"
26 android:focusable="false"
27 />
28 <Button
29 android:layout_width="wrap_content"
30 android:layout_height="wrap_content"
31 android:text="Read"
32 android:onClick="readFile"
33 />
34 </LinearLayout>

 

 1 package com.turboradio.activity;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 import android.app.Activity;
9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.EditText;
12
13 public class FileSaveActivity extends Activity{
14 // 檔案名稱
15 private static final String FILE_NAME = "temp.txt";
16 private EditText writeEdit;
17 private EditText readEdit;
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.file_save);
22 writeEdit = (EditText)findViewById(R.id.write_edit);
23 readEdit = (EditText)findViewById(R.id.read_edit);
24 }
25 /**
26 * 寫檔案
27 */
28 public void writeFile(View v){
29 write(writeEdit.getText().toString());
30 }
31 /**
32 * 讀檔案
33 */
34 public void readFile(View v){
35 readEdit.setText(read());
36 }
37 private String read(){
38 try {
39 FileInputStream fis = openFileInput(FILE_NAME);
40 byte [] bytes = new byte [1024];
41 fis.read(bytes);
42 return new String(bytes);
43 } catch (FileNotFoundException e) {
44 e.printStackTrace();
45 } catch (IOException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49 return null;
50 }
51 private void write(String content){
52 try {
53 FileOutputStream fos = openFileOutput(FILE_NAME,MODE_APPEND);
54 fos.write(content.getBytes());
55 fos.close();
56 } catch (FileNotFoundException e) {
57 e.printStackTrace();
58 } catch (IOException e) {
59 e.printStackTrace();
60 }
61 }
62 }

相關文章

聯繫我們

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