본문 바로가기
안드로이드 웹앱 콘테츠 개발자 양성(국비지원)/Data 저장

Android Studio Data 저장 1 (File, Shared Preference, Data base, web서버)

by 차누감 2019. 9. 24.

1) File - 두 가지로 분류 (Internal Storage, External Storage)

2) Shared Preference

3) Data base

4) Web서버

 

 

 

1) File - 두 가지로 분류 (Internal Storage)

화면 구성은 아래와 같이 똑같이 만들고, 어디에 저장하는 지만 다르게 만들것이다.

 

<실행 화면>

Internal Storage

 

activity_main.xml 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">
 
    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="input data"
        android:inputType="text"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="save"
        android:onClick="clickSave"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="load"
        android:onClick="clickLoad"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="show data"
        android:padding="8dp"/>
 
 
</LinearLayout>
 
 

MainActivity.java 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 
 
 
 
public class MainActivity extends AppCompatActivity {
 
    EditText et;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et=findViewById(R.id.et);
        tv=findViewById(R.id.tv);
 
    }
 
    public void clickSave(View view) {
        String data = et.getText().toString();
        et.setText("");
 
        //읽어온 문자열 data를 내부 메모리(Internal Storage)에 저장
        //file에 저장할 수 있도록 stram을 생성해서 데이터 저장
        //file과 연결하는 Stream을 열어주는 기능을
        //Activity가 이미 메소드로 보유하고 있음.
 
        try {
            FileOutputStream fos=openFileOutput("Data.txt",MODE_APPEND);
            // mode에는 두가지가 있다. MODE_PRIVATE은 덮어쓰기, MODE_APPEND는 이어 붙이기
            //위 바이트스트림(FileOutputStream)을 문자 스트림(Writer)으로 변환
            PrintWriter writer= new PrintWriter(fos);
 
            //writer.write(data+"\n"); //아래 한줄과 같은 의미
            writer.println(data);
            writer.flush();
            writer.close();
 
            Toast.makeText(this"saved",Toast.LENGTH_SHORT).show();
 
        } catch (FileNotFoundException e) {e.printStackTrace();}
 
    }
 
    public void clickLoad(View view) {
 
        try {
            FileInputStream fileInputStream = openFileInput("Data.txt");
            //바이트스트림 -> 문자스트림으로 변환
            InputStreamReader isr= new InputStreamReader(fileInputStream);
            //문자스트림->보조스트림으로 변환
            BufferedReader reader= new BufferedReader(isr);
 
            StringBuffer buffer= new StringBuffer();
            String line =reader.readLine();  //한줄 읽어라
 
            while(true){
                buffer.append(line+"\n");
                line= reader.readLine();
                if(line==nullbreak;
            }
            tv.setText(buffer.toString());
        } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
 
    }
}
 
 
 

 

data->data-> ( 자신이 만든 팩키지명) -> files->(임의로 만든 .txt) 있음

<실행 화면>

지금까지 여기서 앱을 종료하고 다시 켜도 load하면 불러올 수 있다.

그리고 그 데이터가 있는 파일을 없애고 싶으면 앱을 지우면 된다.

댓글