<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="닫기" /> <EditText android:id="@+id/editor" android:layout_width="fill_parent" android:layout_height="fill_parent" android:singleLine="false" android:gravity="top" /> </LinearLayout>
package com.commonsware.android.files; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class ReadWriteFileDemo extends Activity { private final static String NOTES="notes.txt"; private final static String TAG = "ReadWriteFiledemo"; private EditText editor; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editor = (EditText)findViewById(R.id.editor); Button btn = (Button)findViewById(R.id.close); btn.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { //액티비티 종료 //액티비티 종료시에 Activity 생명주기에 의해서 //onPause -> onStop -> onDestroy finish(); } }); } public void onResume(){ super.onResume(); try{ InputStream in = openFileInput(NOTES); if(in!=null){ InputStreamReader tmp = new InputStreamReader(in); BufferedReader reader = new BufferedReader(tmp); String str; StringBuffer buf = new StringBuffer(); while((str = reader.readLine()) != null){ buf.append(str+"\n"); } in.close(); editor.setText(buf.toString()); } }catch(java.io.FileNotFoundException e){ Log.e(TAG, "파일을 호출할 수 없습니다.",e); }catch(Throwable t){ Log.e(TAG, "오류 발생",t); Toast.makeText(this,"예외 : "+t.toString(),2000) .show(); } } public void onPause(){ super.onPause(); try{ OutputStreamWriter out = new OutputStreamWriter(openFileOutput(NOTES,0)); //MODE_PRIVATE :덮어쓰기 //MODE_APPEND : 이어쓰기 out.write(editor.getText().toString()); out.close(); Toast.makeText(this,"데이터를 저장합니다잉~~",4000).show(); }catch(Throwable t){ Toast.makeText(this,"예외 : "+t.toString(),2000).show(); } } }
이 기능은 데이터를 저장하는 어플리케이션에 모두(거의?) 적용되어 있지만 사용자의 불안함(?) 때문에 자동저장이 있어도 저장버튼을 따로 둔다고함
'Android > 기본' 카테고리의 다른 글
XML Resource (0) | 2012.04.28 |
---|---|
StaticFile (0) | 2012.04.28 |
Access Web (0) | 2012.04.28 |
NetWork Wifi , 3G (0) | 2012.04.28 |
Browser WebView (브라우저) (0) | 2012.04.28 |