MainActivity.java

Property File Write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    //Property 파일 쓰기
    public void WriteToProperty(){
        //property 파일
        File file = new File(Environment.getDataDirectory()+"/data/"+getPackageName(), "PropTest.properties");
 
        FileOutputStream fos = null;
        try{
            //property 파일이 없으면 생성
            if(!file.exists()){
                file.createNewFile();
            }
 
            fos = new FileOutputStream(file);
 
            //Property 데이터 저장
            Properties props = new Properties();
            props.setProperty("test" , "Property에서 데이터를 저장");   //(key , value) 로 저장
            props.store(fos, "Property Test");
 
            Log.d("prop""write success");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
cs


Property File Read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    //Property 파일 읽기
    public String ReadToProperty(){
        //property 파일
        File file = new File(Environment.getDataDirectory()+"/data/"+getPackageName(), "PropTest.properties");
 
        if(!file.exists()){ return ""; }
 
        FileInputStream fis = null;
        String data = "";
        try{
            fis = new FileInputStream(file);
 
            //Property 데이터 읽기
            Properties props = new Properties();
            props.load(fis);
            data = props.getProperty("test""");  //(key , default value)
 
            Log.d("prop""read success");
        }catch(IOException e){
            e.printStackTrace();
        }
 
        return data;
    }
cs



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Property파일 읽기/쓰기" />
    </LinearLayout>
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btnPropWrite"
            android:text="Property 쓰기"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mOnPropertyClick" />
        <Button
            android:id="@+id/btnPropRead"
            android:text="Property 읽기"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mOnPropertyClick"  />
    </LinearLayout>
 
    <LinearLayout
        android:paddingBottom="8dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="결과 : " />
        <TextView
            android:id="@+id/txtProperty"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
</LinearLayout>
 
cs



결과

쓰기 버튼을 누르면 Properties 파일에 데이터를 쓰고 읽기버튼을 누르면 Properties 파일에서 데이터를 읽어온다.

 


+ Recent posts