안드로이드

 

 

 

 

 

class MainActivity 
package com.example.choi.mystudy25_1;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

    final static String TAG="MainActivity";
    Button btnOut, btnIn, btnDelete;
    EditText etOut;
    TextView tvIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etOut=(EditText)findViewById(R.id.et_out);
        tvIn=(TextView)findViewById(R.id.tv_in);


        btnOut=(Button)findViewById(R.id.bt_out);
        btnIn=(Button)findViewById(R.id.bt_in);
        btnDelete=(Button)findViewById(R.id.bt_delete);


        btnOut.setOnClickListener(listener);
        btnIn.setOnClickListener(listener);
        btnDelete.setOnClickListener(listener);

    }



    View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()){

                case R.id.bt_out:
                    Log.i(TAG, "out");
                    FileOutputStream fos =null;
                    try{
                        fos=openFileOutput("ex.txt", Context.MODE_PRIVATE);
                        String out =etOut.getText().toString();
                        fos.write(out.getBytes());
                        Toast.makeText(MainActivity.this, "write end", Toast.LENGTH_SHORT).show();

                    }catch (Exception e){
                        e.printStackTrace();
                    }finally {
                        try {
                            if(fos!=null)fos.close();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    break;


                case R.id.bt_in:
                    Log.i(TAG,  "in");
                    FileInputStream fis =null;
                    try{
                        fis=openFileInput("ex.txt");
                        byte[] data=new byte[fis.available()];
                        while (fis.read(data)!=-1){

                        }
                        tvIn.setText(new String(data));
                        Toast.makeText(MainActivity.this, "read end",
                                Toast.LENGTH_SHORT
                                ).show();
                    }catch (Exception e){
                        tvIn.setText("");
                        e.printStackTrace();

                    }finally {
                        try{
                            if(fis!=null)fis.close();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    break;



                case R.id.bt_delete:
                    Log.i(TAG, "delete");
                    boolean b=deleteFile("ex.txt");
                    if(b){
                        Toast.makeText(MainActivity.this, "delete OK!", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this, "delete NG!", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }

        }
    };







}




 

 

R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.choi.mystudy25_1.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bt_out"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp"
        android:id="@+id/tv_in"
        android:textColor="@color/colorAccent" />

    <Button
        android:text="data in"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_in"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:id="@+id/bt_in" />

    <Button
        android:text="data delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="29dp"
        android:id="@+id/bt_delete" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:layout_marginTop="41dp"
        android:id="@+id/et_out"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="@android:color/holo_green_dark" />

    <Button
        android:text="data out"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:id="@+id/bt_out"
        android:layout_below="@+id/et_out"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

 

 

 

 

 

 

 

 

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

아내의 개성과 취미를 존중해 주고 그것을 더욱 발전시키고 실현시킬 수 있도록 하라. -남편십계명-

댓글 ( 4)

댓글 남기기

작성