<mainactivity.xml>

개정 전

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:onClick="mOnclick"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/load"
        android:text="Load"
        android:onClick="mOnclick"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loadres"
        android:text="LoadRes"
        android:onClick="mOnclick"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/delete"
        android:onClick="mOnclick"
        android:text="Delete"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edittext"
        />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:onClick="sendMessage"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/load"
        android:text="Load"
        android:onClick="sendMessage"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loadres"
        android:text="LoadRes"
        android:onClick="sendMessage"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/delete"
        android:onClick="sendMessage"
        android:text="Delete"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edittext"
        />
</LinearLayout>

 

 


<mainactivity.java>

개정 전 

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.view.View;
import android.widget.EditText;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class MainActivity extends AppCompatActivity {

    EditText mEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEdit = (EditText) findViewById((R.id.edittext));



    }
    public void mOnclick(View v)
    {
        switch(v.getId()) {
            case R.id.save:
                try {
                    {
                        FileOutputStream fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
                        String str = "Android File IO TEST";
                        fos.write(str.getBytes());
                        fos.close();
                    }
                }
                catch (Exception e){;}
                break;

            case R.id.load:
            try{
                FileInputStream fis = openFileInput("test.txt");
                byte[] data = new byte[fis.available()];
                while (fis.read(data)!= -1) {;}
                fis.close();
                mEdit.setText(new String(data));

        } catch (FileNotFoundException e) {
                mEdit.setText("File not Found");
            }
            catch (Exception e){;}
            break;

            case R.id.loadres:
                try {
                    InputStream fres = getResources().openRawResource(R.raw.restext);
                    byte[] data = new byte[fres.available()];
                    while (fres.read(data)!= -1){;}
                    fres.close();
                    mEdit.setText(new String(data));
                } catch (Exception e){;}
                break;
            case R.id.delete:
                if(deleteFile("test.txt")) {
                    mEdit.setText("delete Success");
                } else {
                    mEdit.setText("delete Failed");
                }
                break;
    } }
}

개정 후

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.view.View;
import android.widget.EditText;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class MainActivity extends AppCompatActivity {

    EditText mEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEdit = (EditText) findViewById((R.id.edittext));



    }
    public void sendMessage(View v)
    {
        switch(v.getId()) {
            case R.id.save:
                try {
                    {
                        FileOutputStream fos = openFileOutput("test.txt", Context.MODE_PRIVATE);
                        String str = "Android File IO TEST";
                        fos.write(str.getBytes());
                        fos.close();
                    }
                }
                catch (Exception e){;}
                break;

            case R.id.load:
            try{
                FileInputStream fis = openFileInput("test.txt");
                byte[] data = new byte[fis.available()];
                while (fis.read(data)!= -1) {;}
                fis.close();
                mEdit.setText(new String(data));

        } catch (FileNotFoundException e) {
                mEdit.setText("File not Found");
            }
            catch (Exception e){;}
            break;

            case R.id.loadres:
                try {
                    InputStream fres = getResources().openRawResource(R.raw.restext);
                    byte[] data = new byte[fres.available()];
                    while (fres.read(data)!= -1){;}
                    fres.close();
                    mEdit.setText(new String(data));
                } catch (Exception e){;}
                break;
            case R.id.delete:
                if(deleteFile("test.txt")) {
                    mEdit.setText("delete Success");
                } else {
                    mEdit.setText("delete Failed");
                }
                break;
    } }
}

 


res/raw/restext.txt

This file is in resource may be very big

LOAD 시

LoadRes 시

delete 시

 

save 시 저장되는 경로 Tool windows -> Device file Explorer -> 패키지명 -> files 아래에 저장되어 있다.


 

 

참고로 monclick은 deprecation  돼서 더이상 쓰지않는다. 권장 사용법은 아래와 같다

버튼  |  Android 개발자  |  Android Developers

 

버튼  |  Android 개발자  |  Android Developers

버튼 버튼은 사용자가 터치할 때 어떤 작업이 발생하는지 알려주는 텍스트나 아이콘 또는 텍스트와 아이콘으로 구성됩니다. 텍스트나 아이콘 하나만 있는 버튼을 원하는지 또는 둘 다 있는 버

developer.android.com

 

 

 

+ Recent posts