반응형

* 안드로이드 내부 저장소 파일 읽고 쓰고 지우기 예제...

 

메인화면

 

전체 소스 코드

 

package com.example.administrator.androidcontrolex;

import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 InFileExActivity extends AppCompatActivity {

    EditText etInput;
    TextView tvList;

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

        //액션바 셋팅
        ActionBar ab = getSupportActionBar();
        ab.setTitle("내부 파일 예제...");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        etInput = (EditText)findViewById(R.id.txtInput);
        tvList = (TextView)findViewById(R.id.txtList);

        Button btnS = (Button)findViewById(R.id.btnSave);
        Button btnL = (Button)findViewById(R.id.btnLoad);
        Button btnD = (Button)findViewById(R.id.btnDelete);
        Button btnO = (Button)findViewById(R.id.btnOpen);

        btnS.setOnClickListener(lisner);
        btnL.setOnClickListener(lisner);
        btnD.setOnClickListener(lisner);
        btnO.setOnClickListener(lisner);

    }

    public  void FileSave(String strFileName, String strMsg)
    {
        try
        {
            //파일이름 날짜 표현 방법 : String strTime = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            //20180903_112233
            FileOutputStream fos = openFileOutput(strFileName, Context.MODE_APPEND);
            String strText = strMsg + "\n";
            fos.write(strText.getBytes());
            fos.close();
        }
        catch (Exception e)
        {
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
            return;
        }

        Toast.makeText(getApplicationContext(),"파일 저장이 완료 되었습니다.",Toast.LENGTH_SHORT).show();
    }

    public  String FileLoad(String strFileName)
    {
        String strTmp;
        try {
            FileInputStream fis = openFileInput(strFileName);
            StringBuffer sb = new StringBuffer();
            byte dataBuffer[] = new byte[1024];
            int n = 0 ;

            // -1 파일 끝을 의미
            while((n=fis.read(dataBuffer)) != -1 ){
                sb.append(new String(dataBuffer) );
            }

            strTmp = sb.toString();
            fis.close();

        }
        catch (Exception e)
        {
            Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
            return "";
        }

        Toast.makeText(getApplicationContext(),"파일 로드가 완료 되었습니다.",Toast.LENGTH_SHORT).show();
        return  strTmp;
    }

    public  void FileDelete(String strFileName)
    {
        if(deleteFile(strFileName))
        {
            Toast.makeText(getApplicationContext(),"파일 정상적으로 삭제 되었습니다.",Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(),"파일 삭제하는데 실패 하였습니다.",Toast.LENGTH_SHORT).show();
        }
    }


    Button.OnClickListener lisner = new Button.OnClickListener(){
      public void onClick(View vi)
      {
            switch (vi.getId())
            {
                case R.id.btnSave :
                {
                    FileSave("test.txt", etInput.getText().toString());
                    etInput.setText("");
                    break;
                }
                case R.id.btnLoad :
                {
                    tvList.setText(FileLoad("test.txt"));
                    break;
                }
                case R.id.btnDelete :
                {
                    FileDelete("test.txt");
                    break;
                }
                case R.id.btnOpen :
                {

                    break;
                }
            }
      }
    };

  
}

 

파일 Write Source

 

파일 Read Source

파일 Delete Source

 

 

*예제 결과 화면

파일 저장
파일 읽기
파일 삭제
파일이 삭제 된 후 파일 읽기한 화면

 

반응형

+ Recent posts