package com.example.admin.s20172285_0910_01;

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

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

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

        Button btn01=(Button)findViewById(R.id.btn01);
        Button btn02=(Button)findViewById(R.id.btn02);

        btn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    FileOutputStream outFs= openFileOutput("file.txt",
                            Context.MODE_WORLD_WRITEABLE);
                    String str="안드로이드 파일"+'\r'+'\n'+"입출력 테스트";
                    outFs.write(str.getBytes());
                    outFs.close();
                }catch(FileNotFoundException e){
                    e.printStackTrace();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        });
        btn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    FileInputStream inFs= openFileInput("file.txt");
                    byte[] txt =new byte[100];
                    inFs.read(txt);
                    String str =new String(txt);
                    Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
                    inFs.close();
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        });

    }
}
