Search

'ANDROID'에 해당되는 글 1건

  1. 2010.10.15 예제코드

예제코드

ANDROID 2010. 10. 15. 21:27 Posted by Dayis

package study.chapter07.fileex;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FileEx extends Activity implements View.OnClickListener {
 private EditText editText;
 private Button btnWrite;
 private Button btnRead;
 
 public void onCreate(Bundle icicle){
  super.onCreate(icicle);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  
  LinearLayout layout = new LinearLayout(this);
  layout.setBackgroundColor(Color.rgb(255,255,255));
  layout.setOrientation(LinearLayout.VERTICAL);
  setContentView(layout);
  
  editText = new EditText(this);
  editText.setText("",EditText.BufferType.NORMAL);
  setLLParams(editText,240,50);
  layout.addView(editText);
  
  btnWrite = new Button(this);
  btnWrite.setText("쓰기");
  btnWrite.setOnClickListener(this);
  setLLParams(btnWrite);
  layout.addView(btnWrite);
  
  btnRead = new Button(this);
  btnRead.setText("읽기");
  btnRead.setOnClickListener(this);
  setLLParams(btnRead);
  layout.addView(btnRead);
  
 }

 private void setLLParams(Button btnWrite2) {
  // TODO Auto-generated method stub
  
 }

 private void setLLParams(EditText editText2, int i, int j) {
  // TODO Auto-generated method stub
  
 }

 public void onClick(View v) {
  if (v==btnWrite){
   try {
    String str = editText.getText().toString();
    str2file(this,str,"test.txt");
   } catch (Exception e){
    showDialog(this,"에러","쓰기 실패했습니다.");
   }
  } else if (v==btnRead) {
   try {
    String str = file2str(this,"text.txt");
    editText.setText(str,TextView.BufferType.EDITABLE);    
   } catch (Exception e) {
    showDialog(this,"에러","읽기 실패 했습니다.");
   }
   
  }
  
 }

 private static String file2str(Context context, String fileName) throws Exception {
  byte[] w = file2data(context,fileName);
  return new String(w);
 }

 private static byte[] file2data(Context context, String fileName) throws Exception {
  int size;
  byte[] w = new byte[1024];
  InputStream in = null;
  ByteArrayOutputStream out = null;
  try {
   in = context.openFileInput(fileName);
   out = new ByteArrayOutputStream();
   while(true){
    size=in.read(w);
    if (size<=0)
     break;
    out.write(w,0,size);
   }
   out.close();
   in.close();
   return out.toByteArray();
  } catch (Exception e){
   try {
    if(in!=null)
     in.close();
    if(out!=null)
     out.close();
   } catch (Exception e2){
    
   }
   throw e;
  }
 }

 private static void showDialog(final Activity activity,String title, String text) {
  AlertDialog.Builder ad = new AlertDialog.Builder(activity);
  
 }

 private static void str2file(Context context, String str, String fileName) throws Exception {
  // TODO Auto-generated method stub
  data2file(context,str.getBytes(),fileName);
 }

 private static void data2file(Context context, byte[] w, String fileName) throws Exception {
  OutputStream out = null;
  try {
   out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
   out.write(w,0,w.length);
   out.close();
  } catch (Exception e) {
   try {
    if(out != null)
      out.close();
   } catch (Exception e2) {    
   }
   throw e;
  }  
 }

}