반응형

 EditText 에 입력한 글씨를 내부 메모리(Internal Storage)에 저장하고 다시 읽어오는 소스입니다.



메인화면                                       내부메모리에 저장                            내부메모리에서 읽어오기

         



Layout 파일

 activity_main.xml

 <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">

    

    <EditText android:id="@+id/edit"

        android:layout_width="match_parent"

        android:layout_height="50dp"

        android:padding="5dp"

        android:inputType="text"

        android:hint="input text"/>

    

    <Button android:id="@+id/btn_save"

        android:layout_width="match_parent"

        android:layout_height="50dp"

        android:text="save file into Internal"

        android:onClick="mOnClick"/>

    

    <Button android:id="@+id/btn_load"

        android:layout_width="match_parent"

        android:layout_height="50dp"

        android:text="load file from Internal"

        android:onClick="mOnClick"/>

    

    <TextView android:id="@+id/text"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:padding="5dp"

        android:text="Show data from file"/>


</LinearLayout>



소스파일

 

 MainActivity.java

 public class MainActivity extends Activity {

EditText edit;

TextView text;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

edit= (EditText)findViewById(R.id.edit);

text= (TextView)findViewById(R.id.text); 

}

//Button을 클릭했을 때 자동으로 호출되는 callback method....

public void mOnClick(View v){

switch(v.getId()){

case R.id.btn_save: //Internal Storage에 file 저장하기

String data= edit.getText().toString(); //EditText에서 Text 얻어오기

edit.setText("");

try {

//FileOutputStream 객체생성, 파일명 "data.txt", 새로운 텍스트 추가하기 모드

FileOutputStream fos=openFileOutput("data.txt", Context.MODE_APPEND); 

PrintWriter writer= new PrintWriter(fos);

writer.println(data);

writer.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//소프트 키보드 없애기

InputMethodManager imm= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

break;

case R.id.btn_load: //file 에서 읽어오기

StringBuffer buffer= new StringBuffer();

try {

//FileInputStream 객체생성, 파일명 "data.txt"

FileInputStream fis=openFileInput("data.txt");

BufferedReader reader= new BufferedReader(new InputStreamReader(fis));

String str=reader.readLine();//한 줄씩 읽어오기

while(str!=null){

buffer.append(str+"\n");

str=reader.readLine();

}

text.setText(buffer.toString());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

}

}

}



 

저장된 위치 확인 (Eclipse 에서 DDMS의 본인 앱위치)

- 본인의 앱 폴더 안에 있으므로 앱을 제거하면 파일도 같이  제거됩니다.











반응형

+ Recent posts