class ReadWriteActivity
package com.example.choi.ex04_file;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ReadWriteActivity extends AppCompatActivity {
//1.변수선언
Button btnSave, btnLoad, btnDelete;
EditText edit1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.read_write);
//2.위젯 연결
btnSave =(Button)findViewById(R.id.btnSave);
btnLoad=(Button)findViewById(R.id.btnLoad);
btnDelete=(Button)findViewById(R.id.btnDelete);
edit1=(EditText)findViewById(R.id.edit1);
//3.저장 버튼
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// /data/data/패키지이름/files => 내장 메모리 전용 디렉토리
File file =new File("/data/data/com.example.choi.ex04_file/files/test.txt");
try{
//스트림 - 1바이트 단위 입출
// Reader, Writer 문자 단위 입출력
FileOutputStream fos =new FileOutputStream(file);
// 사용자가 입력한 내용
String str =edit1.getText().toString();
//바이트배열로 변환하여 저장
fos.write(str.getBytes());
fos.close();//스트림 닫기
Toast.makeText(ReadWriteActivity.this,
"저장되었습니다.", Toast.LENGTH_LONG).show();
}catch (Exception e){
e.printStackTrace();
}
}
});
//4. 불러오기 버튼 클릭
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
// opendFileInput() 내장 메모리의 파일 오픈
// /data/data/패키지이름/files
InputStream is=openFileInput("test.txt");
if(is!=null){
//스트림을 리더로(문자 단위, 한글 처리)
InputStreamReader reader=
new InputStreamReader(is, "utf-8");
BufferedReader br =new BufferedReader(reader);
String str="";
StringBuffer sb =new StringBuffer();
//br.readLine() 버퍼에서 한 라인을 읽음
while ((str =br.readLine()) !=null){
sb.append(str +"\n"); //개행문자 추가
}
is.close(); //스트림 닫기
edit1.setText(sb.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
});
//5. 삭제하기
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//내장 메모리의 파일 삭제
deleteFile("test.txt");
// Toast.LENGTH_LONG 2~3초
// Toast.LENGTH_SHORT 1 ~ 2초
Toast.makeText(ReadWriteActivity.this, "삭제되었습니다.",
Toast.LENGTH_LONG).show();
}
});
}
}
read_write.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/read_write"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex04_file.ReadWriteActivity">
<Button
android:text="저장"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/btnSave" />
<Button
android:text="불러오기"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/btnLoad" />
<Button
android:text="삭제"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/btnDelete" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/edit1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_below="@+id/btnSave" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기